Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
#12614: ManyToManyField generates inconsistent table/column names
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Django  
View profile  
 More options Jan 14 2010, 6:29 pm
From: "Django" <nore...@djangoproject.com>
Date: Thu, 14 Jan 2010 23:29:50 -0000
Local: Thurs, Jan 14 2010 6:29 pm
Subject: [Django] #12614: ManyToManyField generates inconsistent table/column names
#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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Django  
View profile  
 More options Jan 14 2010, 6:36 pm
From: "Django" <nore...@djangoproject.com>
Date: Thu, 14 Jan 2010 23:36:11 -0000
Local: Thurs, Jan 14 2010 6:36 pm
Subject: Re: [Django] #12614: ManyToManyField generates inconsistent table/column names
#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  
        Resolution:                                            |      Keywords:        
             Stage:  Unreviewed                                |     Has_patch:  0    
        Needs_docs:  0                                         |   Needs_tests:  0    
Needs_better_patch:  0                                         |  
---------------------------------------------------------------+----------- -
Changes (by Nick Retallack <nickretall...@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 <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Django  
View profile  
 More options Jan 14 2010, 6:55 pm
From: "Django" <nore...@djangoproject.com>
Date: Thu, 14 Jan 2010 23:55:31 -0000
Local: Thurs, Jan 14 2010 6:55 pm
Subject: Re: [Django] #12614: ManyToManyField generates inconsistent table/column names
#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  
        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 <nickretall...@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.mod...
 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 <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Django  
View profile  
 More options Jan 14 2010, 9:20 pm
From: "Django" <nore...@djangoproject.com>
Date: Fri, 15 Jan 2010 02:20:04 -0000
Local: Thurs, Jan 14 2010 9:20 pm
Subject: Re: [Django] #12614: ManyToManyField generates inconsistent table/column names
#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  
        Resolution:                                            |      Keywords:        
             Stage:  Unreviewed                                |     Has_patch:  0    
        Needs_docs:  0                                         |   Needs_tests:  0    
Needs_better_patch:  0                                         |  
---------------------------------------------------------------+----------- -
Comment (by Nick Retallack <nickretall...@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 <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Django  
View profile  
 More options Jan 14 2010, 10:01 pm
From: "Django" <nore...@djangoproject.com>
Date: Fri, 15 Jan 2010 03:01:18 -0000
Local: Thurs, Jan 14 2010 10:01 pm
Subject: Re: [Django] #12614: ManyToManyField generates inconsistent table/column names
#12614: ManyToManyField generates inconsistent table/column names
---------------------------------------------------------------+----------- -
          Reporter:  Nick Retallack <nickretall...@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>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »