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

时间:2013-04-24 21:02:50

标签: python django django-south django-extensions

通常情况下,我可以解决突然出现的数据库迁移问题,但这个问题令我感到困惑。

我创建了一个新模型,其中UUIDField(来自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)

0 个答案:

没有答案