[Django] #12614: ManyToManyField generates inconsistent table/column names

0 views
Skip to first unread message

Django

unread,
Jan 14, 2010, 6:29:50 PM1/14/10
to djang...@holovaty.com, django-...@googlegroups.com
#12614: ManyToManyField generates inconsistent table/column names
------------------------------------------------------+---------------------
Reporter: Nick Retallack <nickre...@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.

Django

unread,
Jan 14, 2010, 6:36:11 PM1/14/10
to djang...@holovaty.com, django-...@googlegroups.com
#12614: ManyToManyField generates inconsistent table/column names
---------------------------------------------------------------+------------
Reporter: Nick Retallack <nickre...@gmail.com> | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: 1.1
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------------------+------------
Changes (by Nick Retallack <nickre...@gmail.com>):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0

Comment:

Oops, typo here:
{{{
### ./manage.py sql column # as sqlite3
}}}
Should have been
{{{
### ./manage.py sql module_name # as sqlite3
}}}
to be consistent with the annotation for the previous code snippet.
Shouldn't affect the issue.

--
Ticket URL: <http://code.djangoproject.com/ticket/12614#comment:1>

Django

unread,
Jan 14, 2010, 6:55:31 PM1/14/10
to djang...@holovaty.com, django-...@googlegroups.com
#12614: ManyToManyField generates inconsistent table/column names
---------------------------------------------------------------+------------
Reporter: Nick Retallack <nickre...@gmail.com> | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: 1.1
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------------------+------------
Comment (by ramiro):

Replying to [ticket:12614 Nick Retallack <nickre...@gmail.com>]:
> If you override the table name in your models and then generate a table
with a ManyToManyField, the results look horribly inconsistent.

It is generating those names as it is described in the documentation:
[http://docs.djangoproject.com/en/1.1/ref/models/fields/#id1 automatic
intermediate m2m table name] and FK fields column names
http://docs.djangoproject.com/en/1.1/ref/models/fields/#database-
representation.

If you want to control the name of the table you can use the `db_table`
field option as
[http://docs.djangoproject.com/en/1.1/ref/models/fields/#django.db.models.ManyToManyField.db_table
documented].

For controlling the FK column names you will need to use a `through` as
you have discovered or crate the table by hand then you will have total
control over its naming.

--
Ticket URL: <http://code.djangoproject.com/ticket/12614#comment:2>

Django

unread,
Jan 14, 2010, 9:20:04 PM1/14/10
to djang...@holovaty.com, django-...@googlegroups.com
#12614: ManyToManyField generates inconsistent table/column names
---------------------------------------------------------------+------------
Reporter: Nick Retallack <nickre...@gmail.com> | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: 1.1
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------------------+------------
Comment (by Nick Retallack <nickre...@gmail.com>):

Replying to [comment:2 ramiro]:
> It is generating those names as it is described in the documentation:
[http://docs.djangoproject.com/en/1.1/ref/models/fields/#id1 automatic
intermediate m2m table name] and FK fields column names
http://docs.djangoproject.com/en/1.1/ref/models/fields/#database-
representation.

Actually it's not consistent with the documentation you linked to. The
"DATABASE REPRESENTATION" paragraph says "By default, this table name is
generated using the names of the two tables being joined. " However, the
table is actually being named after a table and a property, not two
tables.

Also, regardless of what the documentation says, do you think the current
default behavior is sane? Think about this from the perspective of
someone who has to use this database later on, without the django ORM. It
seems silly that "property_name", "modelname", and "othermodelname" all
appear in the generated schema, despite having nothing to do with the
database structure.

--
Ticket URL: <http://code.djangoproject.com/ticket/12614#comment:3>

Django

unread,
Jan 14, 2010, 10:01:18 PM1/14/10
to djang...@holovaty.com, django-...@googlegroups.com
#12614: ManyToManyField generates inconsistent table/column names
---------------------------------------------------------------+------------
Reporter: Nick Retallack <nickre...@gmail.com> | Owner: nobody
Status: closed | Milestone:
Component: Database layer (models, ORM) | Version: 1.1
Resolution: invalid | Keywords:
Stage: Unreviewed | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------------------+------------
Changes (by russellm):

* status: new => closed
* resolution: => invalid

Comment:

Using "other_table_name_table_name" doesn't work because you can have two
m2m fields onto the related table. If you used
"other_table_name_table_name" as the m2m table name, the intermediate
table name would be ambiguous. You need to use the property name to
compose the m2m table name to avoid this ambiguity.

As Ramiro said - if you need control over the m2m table name, use the
db_table option on the m2m field.

--
Ticket URL: <http://code.djangoproject.com/ticket/12614#comment:4>
Reply all
Reply to author
Forward
0 new messages