Modify

Ticket #579 (accepted defect)

Opened 4 years ago

Last modified 3 years ago

Field name not updated on ManyToMany through table

Reported by: thomas.paul.matthews@… Owned by: andrew
Priority: minor Milestone: 1.0
Component: migrations Version: 0.7.2
Keywords: Cc:

Description

I changed the model that a ManyToMany? field pointed to after the 'through' table already existed and deleted the old model. The through table field name still reflects the name of the old model even after schema migration. Opening the migrations before and after the change, I see that the variable models has been updated to reflect the change. Adding a list of model objects to the ManyToMany? field will fail because Django will expect the field name in the 'through' table to be that of the new model, and you get an error like:

_mysql_exceptions.OperationalError?
OperationalError?: (1054, "Unknown column 'user_id' in 'field list'")

Thanks!

Attachments

Change History

comment:1 follow-up: ↓ 2 Changed 4 years ago by andrew

  • Status changed from new to accepted
  • Milestone set to 0.7.3

"The through table field name still reflects the name of the old model even after schema migration"

I thought you said you just swapped out the model - did you also rename the M2M field at the same time? South doesn't do renames particularly well...

comment:2 in reply to: ↑ 1 Changed 4 years ago by thomas.paul.matthews@…

Replying to andrew:

"The through table field name still reflects the name of the old model even after schema migration"

I thought you said you just swapped out the model - did you also rename the M2M field at the same time? South doesn't do renames particularly well...

I did not change the name of the M2M field, just the model that the M2M field linked to. For example, I changed:

class Test(models.Model):
    user = models.ManyToManyField(CustomUser)

Intermediate Table is named <app>_test_user with fields:
1) test_id
2) customuser_id

to

class Test(models.Model):
    user = models.ManyToManyField(User)

Intermediate Table is named <app>_test_user (ok) with fields:
1) test_id
2) customuser_id (Django expects it to be user_id)

I was able to create a custom migration and used db.rename_column to fix the issue, but it seemed like it could be handled automatically.

Thanks.

comment:3 Changed 3 years ago by andrew

  • Milestone changed from 0.7.3 to 1.0
View

Add a comment

Modify Ticket

Action
as accepted
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.