On Sat, Feb 21, 2009 at 23:58, Sean <[email protected]> wrote:
> I'm creating an entity as follows:
> class Member(MyEntity):
>
> using_options(tablename='members')
> role = ManyToOne('models.Role', primary_key=True)
> user = ManyToOne('models.User', primary_key=True)
> groups = ManyToMany('models.Group', onupdate='cascade',
> ondelete='cascade')
>
> This almost does everything I want. However, I would love to enforce a
> rule in the database that says a user can only have one role in a
> group. This could be accomplished by putting a unique constraint on
> the user and group column in the table created for the many to many
> relationship. Is there some way to do this with Elixir or am I going
> to have to get more familiar with sqlalchemy mappers?
There are two ways you can accomplish this in Elixir:
1) To use the "association object" pattern. That is not use a
ManyToMany relationship at all, and explicitly map the intermediary
table to an Entity.
See http://elixir.ematia.de/trac/wiki/Recipes/UsingEntityForOrderedList,
and http://www.sqlalchemy.org/docs/05/reference/ext/associationproxy.html
for some details.
2) Define the intermediary table manually. And use it like so:
http://elixir.ematia.de/trac/browser/elixir/trunk/tests/test_m2m.py#L224
This however is only supported well in the trunk version of Elixir
(upcoming 0.7).
> To be more clear, the above creates a table called
> members_groups__groups_members with a primary key across all three
> columns: groups_id, members_role_rolename, members_user_username. I'd
> like to put a unique constraint across groups_id and user_username.
--
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
-~----------~----~----~----~------~----~------~--~---