Can I avoid redundant primary key columns in a MySQL table for django (python)?
I am aware of the fact, the if I use the ORM of Django every table has to have a primary key column. Somehow if you have a many_to_many table which links to tables (let's call them authors and books) you would get something like:
id author_id book_id
1 1 1
2 1 2
3 2 3
etc.
I have encountered a book in whic开发者_开发技巧h it is proposed to avoid the column "id" and to create a compound primary key instead. Does this work out with django?
You could create a compound primary key on your through table (with ALTER TABLE). You could also drop the id column from the table. None of this would harm django in way since the way ManyToMany fields work in the backend they wouldn't use the id column anyway.
However you should note that getting compound PK to work in django is basically a non starter. This shouldn't be an issue for you as no table should have a ForeignKey to your through table (for any reason that I can think of at least.
So in summary. Compound primary keys don't work with django. So if you ever need to have a ForeignKey to a table with a compound PK you are basically SOL. Finally there is no real pro about using a compound PK here, but no real con either (in this one and only case). So why are you spending your time worrying about this?
精彩评论