Opened 9 years ago

Closed 9 years ago

#25494 closed Bug (needsinfo)

db_table option doesn't work in ManyToMany fields which using through option

Reported by: Mehran Akhavan Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords: db_table migration
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a model which contains a ManyToMany field using the through option. I wanted to change the name of the many to many table, so I added db_table in Meta class of through model. After running makemigrations and migrate commands, I have a tables with default names.

class MyModel(models.Model):
    my_field = models.ManyToManyField(AnotherModel, through='IntermediateModel')

class IntermediateModel(models.Model):
    class Meta:
         db_table = 'my_intermediate_name'
     my_model = models.ForeignKey(MyModel)
     another_model = models.ForeignKey(AnotherModel)

I run these commands:

python manage.py makemigrations
python manage.py migrate

I'm using postgresql 9.4.4

Change History (1)

comment:1 by Simon Charette, 9 years ago

Resolution: needsinfo
Status: newclosed

Hi mehranakk,

I'm afraid I cannot reproduce the reported issue against the stable/1.8.x and master branches using PostgreSQL with the provided details.

Here's the two scenarios I've tried:

In two steps

  1. Define the models with no explicit table name for the intermediary model.
  2. Create the initial migration via makemigration.
  3. Alter the intermediary model definition to include an explicit table name via the db_table option.
  4. Create the second migration using makemigration and assert it contains the appropriate AlterModelTable operation.
  5. Run the migrations using migrate and assert the intermediary table has the explicit table name as desired.

In one step

  1. Define the models with an explicit table name for the intermediary table via the db_table option.
  2. Create the initial migration via makemigration and assert the CreateModel operation has the correct db_table defined.
  3. Run the migrations using migrate and assert the intermediary table has the explicit table name as desired.

Please re-open this ticket if you can provide more info. A detailed step by step description of the actions performed would help.

Note: See TracTickets for help on using tickets.
Back to Top