ManyToMany programming error
|Reported by:||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.0|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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.
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'
When I execute the following code:
store_config = DBStore.objects.get(id=1) values = store_config.products.all()
I get the following Programming Error:
ProgrammingError column storeproducts.product_id_id does not exist LINE 1: ...product" INNER JOIN "storeproducts" ON ("product"."id" = "storeproducts...
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.
To sum up when I've changed the model and set the database column name for product id
product_id = models.ForeignKey(DBProduct, db_column='product_id' primary_key=True)
everything seems to go smoothly, so that's obviously ManyToMany field bug.
I'm using Django 1.0.2 (stable release), Python 2.6.1 for Windows.
(BTW. All above examples have been written by hand since I needed to change my quite a secret
table names ;) Sorry for spelling mistakes)