"secondary" requires a Table object as its argument.   it is not
recommended to create a relation that uses a mapped table as its
"secondary" unless the relation specifies viewonly=True.



Jae Kwon wrote:
>
> Is there a way to declaratively create many to many relationships
> where the 'secondary' parameter for the relationship is deferred ?
>
> I couldn't get this to work, e.g.
>
> class User(DeclarativeBase):
>      id = Column(Integer, primary_key=True)
>      name = Column(String(20))
>      groups = relation("Group", primaryjoin=("User.id ==
> GroupMember.user_id"), secondaryjoin=("GroupMember.group_id ==
> Group.id"), secondary="GroupMember")
>
> (the other classes are defined later).
>
> I was able to get around this with the following patch.
>
> --- a/sqlalchemy0.5/lib/sqlalchemy/orm/properties.py  Mon Aug 31
> 22:37:21 2009 -0700
> +++ b/sqlalchemy0.5/lib/sqlalchemy/orm/properties.py  Tue Sep 01
> 22:11:07 2009 -0700
> @@ -736,7 +745,11 @@
>           # accept callables for other attributes which may require
> deferred initialization
>           for attr in ('order_by', 'primaryjoin', 'secondaryjoin',
> 'secondary', '_foreign_keys', 'remote_side'):
>               if util.callable(getattr(self, attr)):
> -                setattr(self, attr, getattr(self, attr)())
> +                called_value = getattr(self, attr)()
> +                # the 'secondary' param requires a table, not a
> declarative class...
> +                if attr == 'secondary' and hasattr(called_value,
> '__mapper__'):
> +                    called_value = called_value.__mapper__.mapped_table
> +                setattr(self, attr, called_value)
>
>           # in the case that InstrumentedAttributes were used to
> construct
>           # primaryjoin or secondaryjoin, remove the "_orm_adapt"
> annotation so these
>
> - Jae
>
> >
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to