On Mar 29, 2012, at 11:53 AM, Michael Bayer wrote:
>
> There's ways to map to a join of three tables though this wouldn't
> necessarily perform spectacularly, normally here I'd have one class be the
> "lead" class and then have it maintain the other two based on state. If
> UserProfile is your lead class, you can maybe use relationship() to keep
> around the other two, then use events like @validates to update other
> attributes:
>
> from sqlalchemy.orm import validates
>
> class UserProfile(...):
> def __init__(self, ...):
> self.mmfuser = MMFUser(...)
> self.authuser = AuthUser(...)
>
> mmfuser = relationship("MMFUser",
>
> primaryjoin="UserProfile.username==MMFUser.username",
> foreign_keys="UserProfile.username",
> )
> authuser = relationship("AuthUser",
>
> primaryjoin="AuthUser.username==UserProfile.username",
> foreign_keys="UserProfile.username")
>
> @validates('password')
> def update_password(self, key, value):
> self.mmfuser.password = MD5(value)
> self.authuser.auth_password = SHA1(value)
> return crypt(value)
>
> Above I made the relationship a many-to-one from UserProfile to the remote
> class, in fact I've never tried that pattern where UserProfile.username links
> out to two different relationships like that but I think it should work.
>
> Of course you can do one or both of the password/username synchronization it
> in SQL too using triggers, though the result of a trigger wouldn't be present
> in memory until you re-emitted a SELECT statement for the various other
> attributes.
On second thought, the above should work but you'd need to put a @validates on
'username' also.
Since 'username' is not a primary key, I'd probably switch those relationships
to be one-to-one, so whatever you set for UserProfile.username is copied into
MMFUser, AuthUser....now I'm curious though I might try it:
mmfuser = relationship("MMFUser",
primaryjoin="UserProfile.username==MMFUser.username",
foreign_keys="MMFUser.username",
uselist=False
)
authuser = relationship("AuthUser",
primaryjoin="AuthUser.username==UserProfile.username",
foreign_keys="AuthUser.username",
uselist=False
)
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" 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/sqlalchemy?hl=en.