On Mon, May 18, 2009 at 2:13 AM, Russell Keith-Magee <freakboy3...@gmail.com> wrote:
> When you have a symmetrical m2m relation to self, Django needs to > store 2 rows in the m2m table to fully represent the relation - that > is, when you store a 'friendship' between Alice and Bob, Django stores > the relationship (alice, bob) and the relationship (bob, alice). > > This poses a problem when you are using an intermediate m2m table: > what happens to the other data in the m2m table? Do you duplicate the > intermediate data? How do you guarantee consistency? If I modify the > m2m relation in one direction, what updates the row in the other > direction? > > If anyone comes up with a creative way to address this issue, I'm > certainly willing to entertain those ideas. However, in the interim, > Django takes the conservative path, preventing behaviour that is > predictably problematic. > Hi Russ, never thought of this... But... If we have a relation between Alice and Bob, we could save just one relationship with the intermediate data, and when we access the symmetrical m2m field, we could use an UNION to retrieve relations from both Alice and Bob. For example in this situation: class Person(models.Model): friends = models.ManyToManyField("self", through="Friendship") class Friendship(models.Model): person_a = models.ForeignKey(Person) person_b = models.ForeignKey(Person, related_name="_unused_") and p is a Person with some friends, accessing p.friends could result in something like: SELECT DISTINCT person_a as friend_id FROM friendship WHERE person_b = p.pk UNION SELECT DISTINCT person_b as friend_Id FROM friendship WHERE person_a = p.pk Probably this could be done without the UNION using an OR, but I'm no sql guru :( I think this is crucial to represent the typical "friendship" model, infact Pinax is currently using some workarounds to overcome this limitation. What do you think? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---