specify the foreign keys in a ManyToManyField created table?

13 views
Skip to first unread message

eiriks

unread,
Jul 4, 2012, 9:39:40 AM7/4/12
to django...@googlegroups.com
I have a model that auto created an auto inc id field as primary key. It related smoothly with my person model, but when I try to create a relation to committees (Komiteer) I get a new intermediate table with (an auto inc primary key, that's ok, a working relation to commitees, smooth and a non working wrong data type field that I'd expect to link to persons)

Heres the model with the ManyToManyField:

class Dagensrepresentanter(models.Model):
    person = models.ForeignKey(Personer, related_name='dagensrepresentanter_person')
    vara_for = models.ForeignKey(Personer, blank=True, null=True, related_name='dagensrepresentanter_vara_for')
    komiteer = models.ManyToManyField(Komiteer)

it generats this intermediate table (I only included the fields for simplicity):

  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dagensrepresentanter_id` int(11) NOT NULL,
  `komiteer_id` varchar(150) NOT NULL,

The line in italics is a int(11), and probably links fine with the auto generated id in the model for Dagensrepresentanter but it need to link to the person field. In the person table the datatype for this value is varchar(33) NOT NULL

How do I specify that the foreign key in the intermediate table is the person field from the Dagensrepresentanter model? 

eiriks

unread,
Jul 4, 2012, 10:43:01 AM7/4/12
to django...@googlegroups.com
I've tried (among many things) to set the person in Dagensrepresentanter to primary_key=True.  

person = models.ForeignKey(Personer, primary_key=True, related_name='dagensrepresentanter_person')  

to force the relationship to include the "right" id. This seems to do just that. I'm unsure if this has any other implications for me, but as for now I'll settle with this.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages