views:

90

answers:

1

I have a legacy database with a table storing a many-to-many relationship, but without a single primary key column. Is there any way to convince Django to use it anyway?

Schematically:

Product 1<---->* Labeling *<---->1 Label

The Labeling table uses (product_id,label_id) as a compound primary key, and I don't see any way to inform Django about this. (Just using through gives me Unknown column 'labeling.id' in 'field list'.)

Do I need to fall back to custom SQL? Or am I missing something?

A: 

hope this helps you,

http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together

http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.db_index

Ashok
I didn't expect it would... but it did: `unique_together` is all that is needed to stop Django asking for a primary key column. If you add that (or words to that effect) to your answer I can accept it. (Afaik `db_index` is irrelevant: it applies only to individual fields.)
Tikitu
While it is true that Django doesn't complain anymore if you add unique_together *but* it is still not fully functional. For example delete (Model.Delete()) on the model in specified in the through parameters will except. I'm expert enough to dare gives this as an answer, for what I read Django doesn't support no having a primary key on a Model.
Boaz