Reputation: 2876
I have Person
s and Proposal
s. Now I want to store the Person's
votes on Proposals
. To do so, I'll create another class which will provide an "intermediary" table. The goal is to link a Person
with a Proposal
and associate a vote direction
(upvote or downvote). Code below:
class Persons(models.Model):
#code
class Proposal(models.Model):
#code
class Vote(models.Model):
"""A Person (who) votes up or down (direction) a Proposal (what)."""
who = models.ForeignKey(Person)
what = models.ForeignKey(Proposal)
direction = models.BooleanField() # true = upvote = vote in favor
I wanted the PrimaryKey of Vote to be (who, what) together. Instead, Django created a table with an default AUTO-INCREMENTED id
as PrimaryKey. How can I set a ForeignKey (or a set of ForeignKeys) as PrimaryKey?
Upvotes: 1
Views: 3683
Reputation: 51
I too had a similar problem and i was able to work around it by using unique_together meta (in your case under class Vote). read more about unique_together at
https://docs.djangoproject.com/en/dev/ref/models/options/
Upvotes: 4
Reputation: 12420
unfortunately you may be stuck using SQLAlchemy since the standard ORM is not capable of this.
Upvotes: 3
Reputation: 34573
Django ORM does not support composite primary keys: https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys
Upvotes: 4