﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
12614	ManyToManyField generates inconsistent table/column names	Nick Retallack <nickretallack@…>	nobody	"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"")
);
}}}
"		closed	Database layer (models, ORM)	1.1		invalid			Unreviewed	0	0	0	0	0	0
