views:

189

answers:

2

i have a model that is having multiple many to many relation to another model it is as follows:

class Match(models.Model):
"""Model docstring"""
   Match_Id = models.AutoField(primary_key=True)
   Team_one = models.ManyToManyField('Team',related_name='Team one',symmetrical=False,) 
   Team_two = models.ManyToManyField('Team',related_name='Team two',symmetrical=False,) 
   stadium = models.CharField(max_length=255, blank=True)
   Start_time = models.DateTimeField(auto_now_add=False, auto_now=False, blank=True, null=True)
   Rafree = models.CharField(max_length=255, blank=True)
   Judge = models.CharField(max_length=255, blank=True)
   winner = models.ForeignKey('Team', related_name='winner',to_field='Team_Name')    
   updated = models.DateTimeField('update date', auto_now=True )
   created = models.DateTimeField('creation date', auto_now_add=True )

what is the best way to implement model like this ?. all though django does not throw any errors when passing the model sql once syncdb is excuted it throws up errors saying there is no unique constraint matching given keys

+1  A: 

Are you sure Team_one and Team_two should be ManyToMany fields? Surely, a match only has a single team on each side - in which case these should both be ForeignKeys.

Daniel Roseman
your are right it should be foreignkeys. I have changed it to foriegnkeys but still getting the same `there is no unique constraint matching given keys`error
nashr rafeeg
+1  A: 

Using spaces in related_name attribute makes me uneasy, but I think the real problem is connected to the use of to_field attribute on the winner field. As far as I know you can set database relations only to unique fields. It doesn't really make sense to relate to another object using a field that may not be unique.

I'm not sure what do you want to achieve by connecting through this particular field. You usually connect models using primary key fields. This still allows you to access any other field on the related object.

Ludwik Trammer
i have actually the `winner` field and the error is still persisting The problem seems that both `team_one` and `team_two` fields is having same relation to `Team` Model.
nashr rafeeg
"i have actually the winner field" - I think you've lost a word in the sentence. "Actually removed"?
Ludwik Trammer
yea sorry bit early in the morning here so missing stuff. I removed the field. Any way i figured out what the problem was. IT was not creating constraints because i had a custom primary key defined as soon as i removed it's working now.
nashr rafeeg