在Django 1.7

时间:2015-10-31 17:38:39

标签: python mysql django

我的项目介绍了Django(v1.7)的内置迁移系统。 我过去常规地迁移了ForeignKeys,除了这个具体案例之外从未遇到任何麻烦。

我有以下迁移,其中包括创建Decision和SubmissionLayer对象。 Decision对象的一些字段(tracks和presentation_types)指向SubmissionLayer对象。

以下是迁移代码本身:

from __future__ import unicode_literals
from django.db import models, migrations
import sots.generic

class Migration(migrations.Migration):
    dependencies = [
        ('main', '0026_submission_abstract_params'),
    ]

    operations = [
        migrations.CreateModel(
            name='Decision',
            fields=[
                    ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                    ('abstract', models.ForeignKey(related_name='decisions', to='main.Abstract')),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='SubmissionLayer',
            fields=[
                    ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                    ('start_date', models.DateTimeField(default=sots.generic.aftertomorrow)),
                    ('end_date', models.DateTimeField(default=sots.generic.aftertomorrow)),
            ],
            options={
                'abstract': False,
            },
            bases=(models.Model,),
        ),
        migrations.AddField(
            model_name='decision',
            name='presentation_type',
            field=models.ForeignKey(related_name='typed_decisions', null=True, to='main.SubmissionLayer', blank=True),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='decision',
            name='track',
            field=models.ForeignKey(related_name='tracked_decisions', null=True, to='main.SubmissionLayer', blank=True),
            preserve_default=True,
        ),
        migrations.AlterUniqueTogether(
            name='decision',
            unique_together=set([('abstract', 'track', 'presentation_type')]),
        ),
        migrations.AddField(
            model_name='abstract',
            name='presentation_types',
            field=models.ManyToManyField(related_name='typed_abstracts', null=True, to='main.SubmissionLayer', blank=True),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='abstract',
            name='tracks',
            field=models.ManyToManyField(related_name='tracked_abstracts', null=True, to='main.SubmissionLayer', blank=True),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='submission',
            name='presentation_types',
            field=models.ManyToManyField(related_name='typed_submission', null=True, to='main.SubmissionLayer', blank=True),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='submission',
            name='tracks',
            field=models.ManyToManyField(related_name='tracked_submission', null=True, to='main.SubmissionLayer', blank=True),
            preserve_default=True,
        ),
    ]

我得到的错误如下:

Applying main.0027_auto_20151022_1635...
Traceback (most recent call last):
File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
...
File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-macosx
10.10-x86_64.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
django.db.utils.IntegrityError: (1215, '**Cannot add foreign key constraint**')

以下是我的MySQL模式对Decision对象的看法:

mysql> DESCRIBE main_decision;
+----------------------+---------+------+-----+---------+----------------+
| Field                | Type    | Null | Key | Default | Extra          |
+----------------------+---------+------+-----+---------+----------------+
| id                   | int(11) | NO   | PRI | NULL    | auto_increment |
| abstract_id          | int(11) | NO   | MUL | NULL    |                |
| presentation_type_id | int(11) | YES  |     | NULL    |                |
| track_id             | int(11) | YES  |     | NULL    |                |
+----------------------+---------+------+-----+---------+----------------+

显然无法创建ForeignKeys presentation_type和track,因为它们在架构中不显示为“MUL”。

0 个答案:

没有答案