2

我想向现有模型添加一列,并且在运行“schemamigration --auto”后,生成的迁移尝试删除另一个表!为什么会这样做?

我的模型(简化)-

class A(Model):
    a = CharField()
    b = BooleanField(default=False)   #  <--- this is the new column

class B(Model):                # <---- this is the table South wants to delete
    c = CharField()
    d = ManyToManyField(A, through='C')

结果迁移——

def forwards(self, orm):
    # Removing M2M table for field d on 'B'
    db.delete_table('B_d')  # <-------  Why is that?
    # Adding field 'b'
    db.add_column('A', 'b', self.gf('django.db.models.fields.BooleanField')(default=False, blank=True), keep_default=False)
4

2 回答 2

1

through="C"关键字告诉模型将C用作关系表。

文档链接:多对多关系中的额外字段

关于 through 关键字的其他 SO 问题:将同一对象两次添加到 ManyToManyField

于 2011-02-13T18:43:06.210 回答
1

您是否还同时/在创建 m2m B_d 之后添加了一个直通模型?没有看到更多代码很难确定,但看起来 South 正在放弃自动连接表,因为现在有一个指定的“通过”模型将用于连接。我还假设模型 C 确实存在 :o)

于 2011-02-13T18:43:19.203 回答