RMittelman RMittelman - 20 days ago 7
MySQL Question

Django Trouble Creating ManyToMany Field

Migrating my Access app to Python/Django. Existing database, so used Inspectdb to create models, which I adjusted as needed until they matched actual db. All models marked Manage = False in Meta.

Now I want to add a many-to-many field in a parent table, and having troubles. Snippets of models:

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')


So I added the "fedpgms" field, then did makemigrations which seemed to work, and then did migrate, which also seemed to work. But, the join table, reports_fedpgms was not created, and when I run the application I get the following error:

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


I tried making the Reports table manage=True, thinking this might trigger creation of the join table, but that only caused an error during migrating, saying "Reports table already exists".

I'm kind of new to this Django thing, what am I doing wrong?

Thanks...

Answer

Change it to

fedpgms = models.ManyToManyField(Federalprograms)
Comments