On Wed, 2008-04-23 at 11:49 -0700, kalve wrote: > Hi, > > I have got a Person model, and I need create a spouse field. > > When editing Jane, I need to be able to John as her spouse. Jane > cannot have more than one spouse, and only one person can have Jane as > spouse. The relation needs to be symmetrical. > > I have tried both ManyToOne and ForeignKey, but I don't seem to be > able to grok this.
Thinking about the data constraints, it's really a many-to-many relation where each column in the intermediate table is unique. That is, a Person model is joined to itself with a many-to-many key via an intermediate database table that stores the pairs of ids of the related models. Each id is only allowed to appear once in that table. There's an extra constraint that anything appearing in the first column cannot appear in the second column (so you can't add John->Jane and Jane->John). Out of the box, you can't do this in Django. However, you can get very close and then add the additional database constraint manually using raw SQL. For example, you could create the many-to-many key manually, using the standard technique in [1]. Or you could wait for #6095 to land in trunk and use that eventually (which is mostly just syntactic sugar for the technique in [1], so don't let that block your development). You can then specify the unique constraint on the intermediate fields, but you will have to add the check constraint to enforce uniqueness between the columns manually. [1] http://www.djangoproject.com/documentation/models/m2m_intermediary/ Regards, Malcolm -- Despite the cost of living, have you noticed how popular it remains? http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---