Django: Ticket #10010: ManyToMany programming errorhttps://code.djangoproject.com/ticket/10010
<p>
I'm rather newbie in the django and python programming, but I'm experienced IT guy.
I tried to define data model matching to existing database (Postgresql)
Below you can find my sample code.
</p>
<pre class="wiki">class DBProduct(models.Model):
name = models.CharField(max_length=128)
class Meta:
db_table = u'product'
class DBStore(models.Model):
name = models.CharField(max_length=256)
sotreproducts = models.ManyToManyField(DBProduct, through='DBStoreProducts')
class Meta:
db_table = u'singlestore'
class DBStoreProducts(models.Model):
store_conf = models.ForeignKey(DBStore, db_column='store_id', primary_key=True)
product_id = models.ForeignKey(DBProduct, primary_key=True)
class Meta:
db_table = u'storeproducts'
</pre><p>
When I execute the following code:
</p>
<pre class="wiki"> store_config = DBStore.objects.get(id=1)
values = store_config.products.all()
</pre><p>
I get the following Programming Error:
</p>
<pre class="wiki">ProgrammingError
column storeproducts.product_id_id does not exist
LINE 1: ...product" INNER JOIN "storeproducts" ON ("product"."id" = "storeproducts...
</pre><p>
I suppose it's related to the foreign keys configuration in the DBStoreProducts table,
since for the 'store_conf' field a database column name is defined explicitly (store_id),
but for the product_id field not. What I expected was to use a database field named the
same as 'product_id' field.
</p>
<p>
To sum up when I've changed the model and set the database column name for product id
</p>
<pre class="wiki"> product_id = models.ForeignKey(DBProduct, db_column='product_id' primary_key=True)
</pre><p>
everything seems to go smoothly, so that's obviously ManyToMany field bug.
</p>
<p>
I'm using Django 1.0.2 (stable release), Python 2.6.1 for Windows.
</p>
<p>
(BTW. All above examples have been written by hand since I needed to change my quite a secret
table names ;) Sorry for spelling mistakes)
</p>
en-usDjangohttps://www.djangoproject.com/s/img/site/hdr_logo.gifhttps://code.djangoproject.com/ticket/10010
Trac 1.2.3James BennettMon, 12 Jan 2009 10:58:56 GMTstatus changed; resolution sethttps://code.djangoproject.com/ticket/10010#comment:1
https://code.djangoproject.com/ticket/10010#comment:1
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>invalid</em>
</li>
</ul>
<p>
I don't see any actual description of a bug here, just the expected behavior for Django when you have a column name that doesn't match the default of the ORM.
</p>
Ticketrmaciejczyk@…Mon, 12 Jan 2009 11:54:01 GMThttps://code.djangoproject.com/ticket/10010#comment:2
https://code.djangoproject.com/ticket/10010#comment:2
<p>
The bug is that when you define ManyToMany intermediate table with below keys:
</p>
<pre class="wiki"> store_conf = models.ForeignKey(DBStore, db_column='store_id', primary_key=True)
product_id = models.ForeignKey(DBProduct, primary_key=True)
</pre><p>
and for one key specify database column name, but for the other not, you get the exception
when you execute the code:
</p>
<pre class="wiki">DBStore.objects.get(id=1)
values = store_config.products.all()
</pre><pre class="wiki">ProgrammingError
column storeproducts.product_id_id does not exist
LINE 1: ...product" INNER JOIN "storeproducts" ON ("product"."id" = "storeproducts...
</pre><p>
According to the doc you don't have to define database column names for all keys and as a default value
the class field name is used, but in my example if you define the db_name for at least one field you must do it for all the rest. Otherwise the exception will occur.
</p>
Ticketrmaciejczyk@…Mon, 12 Jan 2009 11:55:09 GMTstatus changed; resolution deletedhttps://code.djangoproject.com/ticket/10010#comment:3
https://code.djangoproject.com/ticket/10010#comment:3
<ul>
<li><strong>status</strong>
changed from <em>closed</em> to <em>reopened</em>
</li>
<li><strong>resolution</strong>
<em>invalid</em> deleted
</li>
</ul>
TicketRamiro MoralesMon, 12 Jan 2009 12:21:59 GMThttps://code.djangoproject.com/ticket/10010#comment:4
https://code.djangoproject.com/ticket/10010#comment:4
<p>
The <code>DBStoreProducts</code> model has more than one field with <code>primary_key</code> option set to True. It's strange that the pre-flight validation isn't catching it.
</p>
Ticketrmaciejczyk@…Mon, 12 Jan 2009 12:25:28 GMThttps://code.djangoproject.com/ticket/10010#comment:5
https://code.djangoproject.com/ticket/10010#comment:5
<p>
Yes, it's done intentionally, since both these fields have been set in my database as primary key (they are unique together).
</p>
TicketRamiro MoralesMon, 12 Jan 2009 13:04:08 GMThttps://code.djangoproject.com/ticket/10010#comment:6
https://code.djangoproject.com/ticket/10010#comment:6
<p>
Replying to <a class="ticket" href="https://code.djangoproject.com/ticket/10010#comment:5" title="Comment 5">rmaciejczyk@nglogic.com</a>:
</p>
<blockquote class="citation">
<p>
Yes, it's done intentionally, since both these fields have been set in my database as primary key (they are unique together).
</p>
</blockquote>
<p>
But it a unsupported configuration, see <a class="ext-link" href="http://docs.djangoproject.com/en/dev/ref/models/fields/#primary-key"><span class="icon">​</span>http://docs.djangoproject.com/en/dev/ref/models/fields/#primary-key</a>
</p>
<p>
It seems there isn't a check for this in <code>django/core/management/validation.py</code>.
</p>
TicketanonymousMon, 12 Jan 2009 13:12:31 GMThttps://code.djangoproject.com/ticket/10010#comment:7
https://code.djangoproject.com/ticket/10010#comment:7
<p>
Replying to <a class="ticket" href="https://code.djangoproject.com/ticket/10010#comment:6" title="Comment 6">ramiro</a>:
</p>
<blockquote class="citation">
<p>
Replying to <a class="ticket" href="https://code.djangoproject.com/ticket/10010#comment:5" title="Comment 5">rmaciejczyk@nglogic.com</a>:
</p>
<blockquote class="citation">
<p>
Yes, it's done intentionally, since both these fields have been set in my database as primary key (they are unique together).
</p>
</blockquote>
<p>
But it a unsupported configuration, see <a class="ext-link" href="http://docs.djangoproject.com/en/dev/ref/models/fields/#primary-key"><span class="icon">​</span>http://docs.djangoproject.com/en/dev/ref/models/fields/#primary-key</a>
</p>
</blockquote>
<p>
Ok, now I see. Thanks.
</p>
<p>
But I have the following "join" table in my database (there is no way to make a change in the table);
</p>
<pre class="wiki">StoreProducts
-------------
store_id : INTEGER PK
product_id: INTEGER PK
other_field : INTEGER
</pre><p>
See, there is no 'id' column and store_id and product_id are both primary key.
(this is a common approach in many databases according to the SQL reference).
</p>
<p>
Could you tell me how I should define my django model for this table?
</p>
TicketRamiro MoralesMon, 12 Jan 2009 13:31:49 GMTstatus changed; resolution sethttps://code.djangoproject.com/ticket/10010#comment:8
https://code.djangoproject.com/ticket/10010#comment:8
<ul>
<li><strong>status</strong>
changed from <em>reopened</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>invalid</em>
</li>
</ul>
<p>
Replying to <a class="ticket" href="https://code.djangoproject.com/ticket/10010#comment:7" title="Comment 7">anonymous</a>:
</p>
<blockquote class="citation">
<p>
See, there is no 'id' column and store_id and product_id are both primary key. (this is a common approach in many databases according to the SQL reference).
</p>
<p>
Could you tell me how I should define my django model for this table?
</p>
</blockquote>
<p>
Given current I'd say it isn't currently possible to describe that that with Django models. At any rate it' material for django-users mailing list, closing this ticket again.
</p>
Ticket