1

通常,我可以解决突然出现的数据库迁移,但这让我感到困惑。

我创建了一个新模型,以UUIDField(from django-extensions) 作为主键。

# BaseModelNamed is just a mixin which provides some basic fields
# e.g. name, description, slug, created, modified
class UUIDObject(BaseModelNamed):
    uuid = UUIDField(primary_key=True)

我还有另一个模型指的是这个模型:

class ParentModel(BaseModelNamed):
    default_uuidobject = models.OneToOneField(UUIDObject,
        related_name='parent_model', blank=True, null=True)
    uuidobject_collection = models.ManyToManyField(UUIDObject,
        related_name='parent_models', blank=True, null=True)

...以及自动生成的迁移:

def forwards(self, orm):
    # Adding model 'UUIDObject'
    db.create_table('app_uuidobject', (
        ('created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, null=True, blank=True)),
        ('last_modified', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, null=True, blank=True)),
        ('name', self.gf('django.db.models.fields.CharField')(max_length=255, null=True, blank=True)),
        ('description', self.gf('django.db.models.fields.TextField')(null=True, blank=True)),
        ('slug', self.gf('django.db.models.fields.SlugField')(max_length=50, null=True, blank=True)),
        ('uuid', self.gf('django.db.models.fields.CharField')(max_length=36, primary_key=True)),
    ))
    db.send_create_signal('app', ['UUIDObject'])

    # Adding field 'ParentModel.default_uuidobject'
    db.add_column('app_parentmodel', 'default_uuidobject',
                  self.gf('django.db.models.fields.related.OneToOneField')(blank=True, related_name='parent_model', unique=True, null=True, to=orm['app.UUIDObject']),
                  keep_default=False)

但是,当我尝试实际执行迁移时,我不断收到迁移错误:

django.db.utils.DatabaseError: (1005, "Can't create table 'db.#sql-819_2ed' (errno: 150)")

从我破译的内容来看,跑步SHOW ENGINE INNODB STATUS可以提供对问题的更多洞察力,事实上,它揭示了这一点:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
130424 20:34:02 Error in foreign key constraint of table ebdb/#sql-819_2ed:
 FOREIGN KEY (`default_uuidobject_id`) REFERENCES `app_uuidobject` (`uuid`):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.

如何以与 South 兼容的方式使用 UUIDField?

或者,我该如何解决这个迁移冲突?

目前,我正在使用以下软件包:

  • django-extensions==0.9
  • South==0.7.6
  • Django==1.4.1

编辑:
我尝试db_index在迁移中添加和创建索引,但都没有成功:

# ...
    ('uuid', self.gf('django.db.models.fields.CharField')(max_length=36, primary_key=True, db_index=True)),
))
db.create_index('app_uuidobject', ['uuid'], unique=True)
4

0 回答 0