Opened 4 years ago

Last modified 3 years ago

#12810 new Bug


Reported by: strelnikovdmitrij Owned by: nobody
Component: Core (Management commands) Version: master
Severity: Normal Keywords: ManyToManyField
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alex)

Originally from documentation.
The name of the table to create for storing the many-to-many data. If this is not provided, Django will assume a default name based upon the names of the two tables being joined.

It is a little bit unclear, cause if db_table is not provided, Django will create table with name based on the name of the model (table that has the ManyToManyField), and field name that contain this ManyToManyField.

class Product(models.Model):
 color = models.ManyToManyField('ProductColor', null=True, blank=True)
 class Meta:
  db_table = 'eshop_product'

class ProductColor(models.Model):
 name = ...
 class Meta:
  db_table = 'eshop_product_color'

running syncdb
after the creation of 'eshop_product' table Django will create 'eshop_product_color' table that holds relation between Product and ProductColor. The table for ProductColor will never create.

Attachments (0)

Change History (10)

comment:1 Changed 4 years ago by Alex

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Reformatted, please use preview.

comment:2 in reply to: ↑ description Changed 4 years ago by strelnikovdmitrij

don't know how to edit :/

running syncdb. after ...

comment:3 Changed 4 years ago by russellm

  • Component changed from Documentation to
  • milestone set to 1.2
  • Triage Stage changed from Unreviewed to Accepted

This is a validation condition that should be caught - the generated m2m table name should recognize the clash with the manually specified db_table.

comment:4 Changed 4 years ago by coleifer

This one is pretty hard to validate against, since the m2m_db_table gets set when the ManyToManyField contributes to class, and any other models that may cause conflicts might not be loaded yet.

One way to get this to work is to specify db_table on your ManyToManyField::

from django.db import models

class Product(models.Model):
    color = models.ManyToManyField('ProductColor', db_table='eshop_product_color_rel', 
                                   null=True, blank=True)
    class Meta:
        db_table = 'eshop_product'

class ProductColor(models.Model):
    name = models.CharField(max_length=200)
    class Meta:
        db_table = 'eshop_product_color'

Your table gets created correctly:

charles@charles-laptop:~/tmp/test_m2m_collision$ ./ syncdb
Creating table django_session
Creating table eshop_product_color_rel
Creating table eshop_product
Creating table eshop_product_color

comment:5 Changed 4 years ago by ubernostrum

  • milestone changed from 1.2 to 1.3

If this is a bug it's a validation bug -- since the problem is we're not warning that you've set up two things which each want the same table name -- but it's enough of an edge case that I don't think it needs to be in 1.2.

comment:6 Changed 3 years ago by lukeplant

  • Type set to Bug

comment:7 Changed 3 years ago by lukeplant

  • Severity set to Normal

comment:8 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

comment:11 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:12 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

Add Comment

Modify Ticket

Change Properties
<Author field>
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'

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

Note: See TracTickets for help on using tickets.