#12614: ManyToManyField generates inconsistent table/column names
------------------------------------------------------+-------------------- -
Reporter: Nick Retallack <nickretall
...@gmail.com> | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: 1.1
Keywords: | Stage: Unreviewed
Has_patch: 0 |
------------------------------------------------------+-------------------- -
If you override the table name in your models and then generate a table
with a ManyToManyField, the results look horribly inconsistent. Take a
look:
{{{
### module_name/models.py
from django.db import models
# Create your models here.
class ModelName(models.Model):
class Meta:
db_table = "table_name"
class OtherModelName(models.Model):
class Meta:
db_table = "other_table_name"
property_name = models.ManyToManyField(ModelName)
}}}
{{{
### ./manage.py sql column # as sqlite3
BEGIN;
CREATE TABLE "table_name" (
"id" integer NOT NULL PRIMARY KEY
)
;
CREATE TABLE "other_table_name" (
"id" integer NOT NULL PRIMARY KEY
)
;
CREATE TABLE "other_table_name_property_name" (
"id" integer NOT NULL PRIMARY KEY,
"othermodelname_id" integer NOT NULL REFERENCES "other_table_name"
("id"),
"modelname_id" integer NOT NULL REFERENCES "table_name" ("id"),
UNIQUE ("othermodelname_id", "modelname_id")
)
;
COMMIT;
}}}
This is especially annoying if you're working with a legacy database that
already has a consistent naming scheme, or if you care at all what your
tables and columns are named. You would have to add a "through" option to
every ManyToManyField and specify that table manually, since Django
insists on using class names and property names in the table definition
when it generates it itself.
In my opinion, class names and property names should never be used in
database table/column definitions. Only the real table names should be
used. In specific, the many to many table should have looked like this:
{{{
CREATE TABLE "other_table_name_table_name" (
"id" integer NOT NULL PRIMARY KEY,
"other_table_name_id" integer NOT NULL REFERENCES "other_table_name"
("id"),
"table_name_id" integer NOT NULL REFERENCES "table_name" ("id"),
UNIQUE ("other_table_name_id", "table_name_id")
);
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/12614>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.