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 )
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 , 15 years ago
Description: | modified (diff) |
---|
comment:3 by , 15 years ago
Component: | Documentation → django-admin.py |
---|---|
milestone: | → 1.2 |
Triage Stage: | Unreviewed → 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 by , 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 , 15 years ago
milestone: | 1.2 → 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 by , 14 years ago
Type: | → Bug |
---|
comment:7 by , 14 years ago
Severity: | → Normal |
---|
comment:13 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:14 by , 10 years ago
Component: | Core (Management commands) → Core (System checks) |
---|---|
Has patch: | set |
Summary: | ManyToManyField.db_table → Add validation for clashing ManyToManyField.db_table names |
comment:15 by , 10 years ago
Patch needs improvement: | set |
---|
Tests do not run. Please uncheck "Patch needs improvement" when you update it, thanks.
comment:16 by , 8 years ago
Cc: | added |
---|---|
Owner: | changed from | to
Patch needs improvement: | unset |
comment:17 by , 8 years ago
Patch needs improvement: | set |
---|
comment:18 by , 8 years ago
Patch needs improvement: | unset |
---|
comment:19 by , 8 years ago
Patch needs improvement: | set |
---|
comment:20 by , 8 years ago
Patch needs improvement: | unset |
---|
comment:21 by , 8 years ago
Patch needs improvement: | set |
---|
comment:22 by , 8 years ago
Patch needs improvement: | unset |
---|
comment:23 by , 8 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Reformatted, please use preview.