Opened 7 years ago

Closed 4 months 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: master
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 Changed 7 years ago by Alex Gaynor

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

Reformatted, please use preview.

comment:3 Changed 7 years ago by Russell Keith-Magee

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

comment:5 Changed 7 years ago by James Bennett

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 Changed 5 years ago by Luke Plant

Type: Bug

comment:7 Changed 5 years ago by Luke Plant

Severity: Normal

comment:8 Changed 5 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

comment:11 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:12 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:13 Changed 2 years ago by Christopher D'Cunha

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

comment:14 Changed 2 years ago by Tim Graham

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

comment:15 Changed 2 years ago by Tim Graham

Patch needs improvement: set

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

comment:16 Changed 4 months ago by Berker Peksag

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

comment:17 Changed 4 months ago by Simon Charette

Patch needs improvement: set

comment:18 Changed 4 months ago by Berker Peksag

Patch needs improvement: unset

comment:19 Changed 4 months ago by Simon Charette

Patch needs improvement: set

comment:20 Changed 4 months ago by Berker Peksag

Patch needs improvement: unset

comment:21 Changed 4 months ago by Simon Charette

Patch needs improvement: set

comment:22 Changed 4 months ago by Berker Peksag

Patch needs improvement: unset

comment:23 Changed 4 months ago by Simon Charette

Triage Stage: AcceptedReady for checkin

comment:24 Changed 4 months ago by Tim Graham <timograham@…>

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