On Saturday, June 16, 2012 4:52:14 PM UTC+3, Michael Bayer wrote:
>
>
> On Jun 16, 2012, at 9:37 AM, Vladimir Iliev wrote: 
>
> > hi, 
> > 
> > what's wrong with the following mapping ? 
> > 
> > TYPE_DEFAULT = 1 
> > 
> > class BaseFriend(DeclarativeBase): 
> >     
> >     __tablename__ = 'friends' 
> >     __table_args__ = {'mysql_engine': 'InnoDB', 
> >                       'mysql_charset': 'utf8'} 
> >     
> >     type = Column(SmallInteger, primary_key=True) 
> > 
> >     user_id = Column(None, ForeignKey('users.id', ondelete='CASCADE'), 
> primary_key=True) 
> >     
> >     friend_id = Column(None, ForeignKey('users.id', 
> ondelete='CASCADE')) 
> > 
> >     friend_external_id = Column(String(100), primary_key=True) 
> > 
> >     __mapper_args__ = dict( 
> >         polymorphic_on=type, 
> >         polymorphic_identity=TYPE_DEFAULT, 
> >     ) 
> > 
> > TYPE_FACEBOOK = 2 
> > 
> > class FacebookFriend(BaseFriend): 
> >     
> >     __mapper_args__ = dict( 
> >         polymorphic_identity=TYPE_FACEBOOK, 
> >     ) 
> > 
> > User.facebook_friends = relationship(FacebookFriend, 
> >     lazy=False, 
> >     primaryjoin=FacebookFriend.user_id==User.id, 
> >     backref='user' 
> > ) 
> > 
> > when i try to session.query(User).first() i'm getting: 
> > 
> > New instance <FacebookFriend at 0x5b79850> with identity key (<class 
> '...Friend.BaseFriend'>, (2, 75L, u'100001288076626')) conflicts with 
> persistent instance <FacebookFriend at 0x5c59710> 
>
> Two friends with the same identity key, that is, associated with the same 
> type, user_id (and hence User object), and friend_external_id. 
>
> I'm not sure what friend_external_id is but if it just refers to some 
> outside source, I doubt you want want the "user_id" column to be part of 
> the primary key here, that establishes one-to-one, not one-to-many. 
>

what i need is friend model with pk (facebook, user id (multiple users 
might have same external id as friend so it must be part of the key), and 
friend external id (which in this case is facebook uid)) ... i can probably 
just use autoincrement integer for primary key but there would be millions 
friend rows and i prefer if it's tiny. i'm adding and deleting 
FacebookFriend objects without problems but it fails when i try to 
session.query(User) with facebook_friends eagerloaded or 
session.merge(user) if user is loaded from memcached

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/1m3eNDxYORsJ.
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