It’s probably more a generic question about database, than a django one but let’s go.
I have very often what could be considered as 2-to-many relations.
For example, I have a class Match in my project, which is an encounter between two teams.
At first, I was using many-to-many:
Match(Model):
teams = ManyToManyField('Team', related_name='matches') #Always two teams
It ended up being very inefficient for match related pages. Especially in django 1.3, because there is no equivalent to select_related for many_to_many relation. And it’s a bit painful having to iterate when you now there is exactly two elements.
Then I switched to this model :
Match(Model):
teams = ManyToManyField('Team', related_name='matches') #Always two teams
team1 = ForeignKey('Team')
team2 = ForeignKey('Team')
When I want to display match related pages, I can use select_related and display very efficiently the two teams.
And when I’m on the team page, I can follow the “matches” relation to get all the matches as before.
But I find that 3 fields to deal with one relation is totally horrible.
Am I doing it right ? What would you recommend ?
I see few solutions there.
1.
If you’re not anticipating that in future a match will be played between other number of teams than two it may be best for you to just remove a ManyToMany field from your second design:
It’ll remove a redundation in a database and makes
Matchmodifications easier (you don’t have to worry about cohesion betweenteamfield and correspondingteam1,team2fields). It also makes impossible to create a Match with other number of corresponding teams than two, which leads to database consistency.On the other hand you’re losing a flexiblity and it’s harder to write queries, because of two fields to check instead of one.
2.
You may want to use a cache. In fact – those two fields you have are nothing other than that, but moving it to a dedicated cache database can make it more efficient and separates your main database from cached values, which leads to a clearer database design.
3.
Write your own ManyToMany fetcher. It’s not very hard to do, but rather ugly. You have to select teams you’re interested in and then somehow attach them to Match objects, e.g.:
3.5.
New version of Django will have a
prefetch_related()method which is meant to work with ManyToMany queries, but I suppose you don’t have so much time.