Django遗留数据库迁移到许多中间表

时间:2018-09-07 09:36:23

标签: django sequelize.js database-migration

从旧版数据库迁移到Django(v1.11)(来自Sequelize)。

数据库中的中间表没有id(主键属性),因为Sequelize不需要它,但是在Django中它是强制性的。

当前Django设置:

class Address(models.Model):
    keyword = models.CharField(max_length=255)
    city = models.CharField(max_length=255, blank=True, null=True)
    state = models.CharField(max_length=255, blank=True, null=True)
    createdAt = models.DateTimeField(db_column='createdAt', auto_now_add=True)
    updatedAt = models.DateTimeField(db_column='updatedAt', auto_now=True)

    class Meta:
        db_table = 'Addresses'

class CustomerExecutive(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    email = models.CharField(max_length=255)
    addresses = models.ManyToManyField('Address', through='AddressCustomerExecutiveMapping')
createdAt = models.DateTimeField(db_column='createdAt', auto_now_add=True)
updatedAt = models.DateTimeField(db_column='updatedAt', auto_now=True)

    class Meta:
        db_table = 'CustomerExecutives'

class AddressCustomerExecutiveMapping(models.Model):
    address = models.ForeignKey(Address,  db_column='addressId', primary_key=True)
    customer_executive = models.ForeignKey(CustomerExecutive, db_column='customerExecutiveId', primary_key=True)
    createdAt = models.DateTimeField(db_column='createdAt', auto_now_add=True)
    updatedAt = models.DateTimeField(db_column='updatedAt', auto_now=True)

    class Meta:
        db_table = 'AddressCustomerExecutiveMapping'
    unique_together = (('address', 'customer_executive'),)

如果未在primary_keyAddressCustomerExecutiveMapping.address上指定AddressCustomerExecutiveMapping.customer_executive,则会给出主键必需的错误,如果指定,我将无法在该列中添加重复值(很多)。

我该如何解决?

0 个答案:

没有答案