Opened 15 years ago

Closed 8 years ago

#12810 closed Bug (fixed)

Add validation for clashing ManyToManyField.db_table names

Reported by: strelnikovdmitrij Owned by: Berker Peksag
Component: Core (System checks) Version: dev
Severity: Normal Keywords: ManyToManyField
Cc: berker.peksag@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alex Gaynor)

Originally from documentation.
ManyToManyField.db_table
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.

Change History (21)

comment:1 by Alex Gaynor, 15 years ago

Description: modified (diff)

Reformatted, please use preview.

comment:3 by Russell Keith-Magee, 15 years ago

Component: Documentationdjango-admin.py
milestone: 1.2
Triage Stage: UnreviewedAccepted

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 by coleifer, 15 years ago

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$ ./manage.py syncdb
Creating table django_session
Creating table eshop_product_color_rel
Creating table eshop_product
Creating table eshop_product_color

comment:5 by James Bennett, 15 years ago

milestone: 1.21.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 by Luke Plant, 14 years ago

Type: Bug

comment:7 by Luke Plant, 14 years ago

Severity: Normal

comment:8 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

comment:11 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:12 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:13 by Christopher D'Cunha, 10 years ago

Owner: changed from nobody to Christopher D'Cunha
Status: newassigned

comment:14 by Tim Graham, 10 years ago

Component: Core (Management commands)Core (System checks)
Has patch: set
Summary: ManyToManyField.db_tableAdd validation for clashing ManyToManyField.db_table names

comment:15 by Tim Graham, 10 years ago

Patch needs improvement: set

Tests do not run. Please uncheck "Patch needs improvement" when you update it, thanks.

comment:16 by Berker Peksag, 8 years ago

Cc: berker.peksag@… added
Owner: changed from Christopher D'Cunha to Berker Peksag
Patch needs improvement: unset

comment:17 by Simon Charette, 8 years ago

Patch needs improvement: set

comment:18 by Berker Peksag, 8 years ago

Patch needs improvement: unset

comment:19 by Simon Charette, 8 years ago

Patch needs improvement: set

comment:20 by Berker Peksag, 8 years ago

Patch needs improvement: unset

comment:21 by Simon Charette, 8 years ago

Patch needs improvement: set

comment:22 by Berker Peksag, 8 years ago

Patch needs improvement: unset

comment:23 by Simon Charette, 8 years ago

Triage Stage: AcceptedReady for checkin

comment:24 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 0bce2f1:

Fixed #12810 -- Added a check for clashing ManyToManyField.db_table names.

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