woc*_*esa 25 python many-to-many sqlalchemy self-reference
我正在尝试使用SQLAlchemy上的声明来实现自引用的多对多关系.
这种关系代表两个用户之间的友谊.在线我发现(在文档和谷歌中)如何建立一个自我参照的m2m关系,在某种程度上角色是有区别的.这意味着在这个m2m关系中,UserA例如是UserB的老板,所以他将他列为"下属"属性或者你有什么.同样,UserB在'上级'下列出UserA.
这没有问题,因为我们可以用这种方式声明一个backref到同一个表:
subordinates = relationship('User', backref='superiors')
Run Code Online (Sandbox Code Playgroud)
当然,那里的'上级'属性在课堂上并不明确.
无论如何,这是我的问题:如果我想反馈到我正在调用backref的相同属性怎么办?像这样:
friends = relationship('User',
secondary=friendship, #this is the table that breaks the m2m
primaryjoin=id==friendship.c.friend_a_id,
secondaryjoin=id==friendship.c.friend_b_id
backref=??????
)
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为如果A和B交朋友关系角色是相同的,如果我调用B的朋友,我应该得到一个包含A的列表.这是完整的问题代码:
friendship = Table(
'friendships', Base.metadata,
Column('friend_a_id', Integer, ForeignKey('users.id'), primary_key=True),
Column('friend_b_id', Integer, ForeignKey('users.id'), primary_key=True)
)
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
friends = relationship('User',
secondary=friendship,
primaryjoin=id==friendship.c.friend_a_id,
secondaryjoin=id==friendship.c.friend_b_id,
#HELP NEEDED HERE
)
Run Code Online (Sandbox Code Playgroud)
对不起,如果这是太多的文字,我只想尽可能明确地用这个.我似乎无法在网上找到任何参考资料.
zzz*_*eek 24
这是我今天早些时候在邮件列表上暗示的UNION方法.
from sqlalchemy import Integer, Table, Column, ForeignKey, \
create_engine, String, select
from sqlalchemy.orm import Session, relationship
from sqlalchemy.ext.declarative import declarative_base
Base= declarative_base()
friendship = Table(
'friendships', Base.metadata,
Column('friend_a_id', Integer, ForeignKey('users.id'),
primary_key=True),
Column('friend_b_id', Integer, ForeignKey('users.id'),
primary_key=True)
)
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
# this relationship is used for persistence
friends = relationship("User", secondary=friendship,
primaryjoin=id==friendship.c.friend_a_id,
secondaryjoin=id==friendship.c.friend_b_id,
)
def __repr__(self):
return "User(%r)" % self.name
# this relationship is viewonly and selects across the union of all
# friends
friendship_union = select([
friendship.c.friend_a_id,
friendship.c.friend_b_id
]).union(
select([
friendship.c.friend_b_id,
friendship.c.friend_a_id]
)
).alias()
User.all_friends = relationship('User',
secondary=friendship_union,
primaryjoin=User.id==friendship_union.c.friend_a_id,
secondaryjoin=User.id==friendship_union.c.friend_b_id,
viewonly=True)
e = create_engine("sqlite://",echo=True)
Base.metadata.create_all(e)
s = Session(e)
u1, u2, u3, u4, u5 = User(name='u1'), User(name='u2'), \
User(name='u3'), User(name='u4'), User(name='u5')
u1.friends = [u2, u3]
u4.friends = [u2, u5]
u3.friends.append(u5)
s.add_all([u1, u2, u3, u4, u5])
s.commit()
print u2.all_friends
print u5.all_friends
Run Code Online (Sandbox Code Playgroud)
pen*_*ant 11
我需要解决这个相同的问题和混乱约了不少有自引用许多一对多的关系,其中我也继承了User一类Friend类,并运行到sqlalchemy.orm.exc.FlushError.最后,我使用连接表(或辅助表)创建了一个自引用的一对多关系,而不是创建自引用的多对多关系.
如果你考虑它,使用自引用对象,一对多是多对多的.它解决了原始问题中的backref问题.
如果你想看到它的实际应用,我也有一个有效的工作示例.此外,它看起来像github格式现在包含ipython笔记本的gists.整齐.
friendship = Table(
'friendships', Base.metadata,
Column('user_id', Integer, ForeignKey('users.id'), index=True),
Column('friend_id', Integer, ForeignKey('users.id')),
UniqueConstraint('user_id', 'friend_id', name='unique_friendships'))
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(255))
friends = relationship('User',
secondary=friendship,
primaryjoin=id==friendship.c.user_id,
secondaryjoin=id==friendship.c.friend_id)
def befriend(self, friend):
if friend not in self.friends:
self.friends.append(friend)
friend.friends.append(self)
def unfriend(self, friend):
if friend in self.friends:
self.friends.remove(friend)
friend.friends.remove(self)
def __repr__(self):
return '<User(name=|%s|)>' % self.name
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7286 次 |
| 最近记录: |