On Fri, Jun 27, 2008 at 12:17 AM, DanL <[EMAIL PROTECTED]> wrote:

Sorry for the slow reply... I thought I did answer this mail?!? Seems
like it was only in my dreams...

> I think I've found a bug with Elixir in creating self-referential many-
> to-many relationships.  It's quite possible there's a way around it
> using additional sqlalchemy arguments, but what those are is not
> immediately clear to me (if someone knows them, however, I would love
> to hear them!).
>
> The situation can be best described with the following scenario:  I'm
> using MySQL.  I want to create a User Entity.  Such a User has many
> friends, also of type User.  Friendships are "bi-directional", so you
> can't have a "one-way" friendship.  In addition to friends, a User
> also has a list of enemies, also of type User, and the 'enemy'
> relation is similarly bi-directional.
>
> The way to implement this, it seems to me, is to do the following:
>
> ----
> class  User(Entity):
>    using_options(tablename="user")
>
>    friends = ManyToMany('User', inverse="friends")
>    enemies = ManyToMany('User', inverse="enemies")
> ----
> (the inverse statement is required because otherwise Elixir will try
> to link 'friends' and 'enemies' as opposite sides of the relationship
> -- ie B is A's friend but A is B's enemy... quite the passive-
> aggressive group dynamic there)

It's not really a bug. You could call it a misfeature, but it was
intended to work this way. The correct way around your problem is to
specify the inverse relationship explicitly.

class  User(Entity):
    using_options(tablename="user")

    friends = ManyToMany('User', inverse="is_friend_of")
    is_friend_of = ManyToMany('User', inverse="friends")
    enemies = ManyToMany('User', inverse="is_enemy_of")
    is_enemy_of = ManyToMany('User', inverse="enemies")

The thing which is confusing is that the "friends" relationship is
*not* symmetrical in a relational database, even though the word
(arguably) implies it. In other words, you can have somebody who
regards many persons as his friends but those people don't consider
that person to be a friend.


> Anyways, doing this gives me an error like this when create_all-ing:

I'll add an exception to prevent having a relationship as its own
inverse has it is (currently) never a valid pattern.
We could also add the functionality to have symmetric relationship,
which would synchronize both side of the relationship automatically...
That wouldn't be too hard to implement I think, but I've yet to see a
use case in a real-life scenario.

-- 
Gaƫtan de Menten
http://openhex.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to