Django麻烦创建ManyToMany字段

时间:2016-11-14 21:44:36

标签: python mysql django

将我的Access应用程序迁移到Python / Django。现有数据库,所以使用Inspectdb创建模型,我根据需要进行调整,直到它们匹配实际数据库。所有模型都标记为Meta中的Manage = False。

现在我想在父表中添加多对多字段,并且遇到麻烦。模特片段:

class Federalprograms(models.Model):
    fedpgmid = models.IntegerField(db_column='FedPgmID', primary_key=True)  # Field name made lowercase.
    fedpgmname = models.CharField(db_column='FedPgmName', max_length=50)  # Field name made lowercase.
    active = models.IntegerField(db_column='Active', blank=True, null=True, default=0)  # Field name made lowercase.
    seq = models.IntegerField(db_column='Seq', blank=True, null=True, default=0)  # Field name made lowercase.
    loanfee = models.DecimalField(db_column='LoanFee', max_digits=10, decimal_places=5, blank=True, null=True, default=0.00000)  # Field name made lowercase.

class Reports(models.Model):
    reportname = models.CharField(db_column='ReportName', primary_key=True, max_length=50)  # Field name made lowercase.
    reporttitle = models.CharField(db_column='ReportTitle', max_length=50, blank=True, null=True)  # Field name made lowercase.
    datefielddefault = models.IntegerField(db_column='DateFieldDefault', blank=True, null=True)  # Field name made lowercase.
    startdatedefault = models.CharField(db_column='StartDateDefault', max_length=15, blank=True, null=True)  # Field name made lowercase.
    enddatedefault = models.CharField(db_column='EndDateDefault', max_length=15, blank=True, null=True)  # Field name made lowercase.
    detailsdefault = models.BooleanField(db_column='DetailsDefault', max_length=1, blank=True, null=False)  # Field name made lowercase.
    totalsdefault = models.BooleanField(db_column='TotalsDefault', max_length=1, blank=True, null=False)  # Field name made lowercase.
    askfedpgm = models.BooleanField(db_column='AskFedPgm', blank=True, null=False, default=0)  # Field name made lowercase.
    fedpgms = models.ManyToManyField('Federalprograms')

所以我添加了“fedpgms”字段,然后确实makemigrations似乎工作,然后进行迁移,这似乎也有效。但是,没有创建连接表,reports_fedpgms,当我运行应用程序时,我收到以下错误:

ProgrammingError at /admin/fc6/reports/StatusReport/change/
(1146, "Table 'fc5.reports_fedpgms' doesn't exist")

我尝试使Reports表manage = True,认为这可能会触发连接表的创建,但这只会在迁移期间导致错误,并说“Reports表已存在”。

我对这个Django事情有点新鲜,我做错了什么?

...谢谢

1 个答案:

答案 0 :(得分:1)

将其更改为

fedpgms = models.ManyToManyField(Federalprograms)