python - Directional Many-to-many self referencing ORM in SQLAlchemy using Association Pattern -
i tried quite while , didn't find solution specific problem, apologize in advance.
suppose have following model tables users , follows:
________ src ________ | user |-1------------*-| follow | |--------| |--------| | id | | src_id | | | | dst_id | | | | string | |________|-1------------*-|________| dst
notice that there different semantics depending on foreign keys.
i'm trying achieve through "association pattern" (described here), can work. looks this:
class follow(base): __tablename__ = 'follows' # src_id = column(biginteger, foreignkey('users.id'), primary_key=true) dst_id = column(biginteger, foreignkey('users.id'), primary_key=true) src = relationship("user", back_populates="followers", foreign_keys=[src_id]) dst = relationship("user", back_populates="followees", foreign_keys=[dst_id]) kind = column(string(16)) class user(base): __tablename__ = 'users' name = column(string(20)) followers = relationship("uuedge", primaryjoin="user.id==uuedge.dst_id") followees = relationship("uuedge", primaryjoin="user.id==uuedge.src_id")
is possible? doing wrong?
cheers
p.s.
similar question not answer mine:
this how implemented follower relationship in project using join table.
followers = table( 'followers', metadata, column('user_id', integer, foreignkey('users.user_id'), primary_key=true), column('follower_id', integer, foreignkey('users.user_id'), primary_key=true), ) class user(base): __tablename__ = 'users' user_id = column(integer, primary_key=true) followers = relationship( 'user', secondary=followers, cascade='all', lazy='dynamic', primaryjoin=followers.c.user_id == user_id, secondaryjoin=followers.c.follower_id == user_id) # following! user = session.query(user).get(..) follower = user() user.followers.append(follower)
Comments
Post a Comment