Opened 5 years ago

Last modified 10 months ago

#12810 assigned Bug

Add validation for clashing ManyToManyField.db_table names

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

Description (last modified by Alex)

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 (13)

comment:1 Changed 5 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 5 years ago by strelnikovdmitrij

don't know how to edit :/

running syncdb. after ...

comment:3 Changed 5 years ago by russellm

  • Component changed from Documentation to django-admin.py
  • 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 5 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 5 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 4 years ago by lukeplant

  • Type set to Bug

comment:7 Changed 4 years ago by lukeplant

  • Severity set to Normal

comment:8 Changed 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

comment:11 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:12 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:13 Changed 10 months ago by christopherdcunha

  • Owner changed from nobody to christopherdcunha
  • Status changed from new to assigned

comment:14 Changed 10 months ago by timo

  • Component changed from Core (Management commands) to Core (System checks)
  • Has patch set
  • Summary changed from ManyToManyField.db_table to Add validation for clashing ManyToManyField.db_table names

comment:15 Changed 10 months ago by timo

  • Patch needs improvement set

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

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