我有三张桌子:
profile
id, int, pk
name...
role
id, int, pk
name
...
profilerole
role_id int, pk, foreign_key to role.id
profile_id int, pk, foreign_key to role.id
我想写一些东西来加载角色信息,我目前有以下配置文件类:
class profile(Base):
__tablename__ = 'profile'
id = Column(Integer, primary_key=True)
name = Column(String)
password = Column(String)
email = Column(String)
enabled = Column(Boolean)
verified = Column(Boolean)
deleted = Column(Boolean)
# this is the line I need help with...
roles = relationship('roleprofile'
primaryjoin="and_(profile.id==roleprofile.id",
backref="profile")
上面的行将为我提供表格中的角色信息,roleprofile
但我希望它给我的是role
表格中的角色。
这是可能的,我将如何去做?
更新
使用这种关系:
roles = relationship('role', secondary=roleprofile, backref='profiles')
为什么定义这个有效:
roleprofiles = Table('roleprofile', Base.metadata,
Column('role_id', Integer, ForeignKey('role.id')),
Column('profile_id', Integer, ForeignKey('profile.id'))
)
如果没有:
class roleprofile(Base):
__tablename__ = 'roleprofile'
role_id = Column(Integer, ForeignKey('role.id'), primary_key=True)
profile_id = Column(Integer, ForeignKey('profile.id'), primary_key=True)
def __init__(self, name, created_by, last_updated_by, created=datetime.now(), last_updated=datetime.now()):
self.name = name
self.created = created
self.created_by = created_by
self.last_updated = last_updated
self.last_updated_by = last_updated_by
使用roleprofile
when already defined 定义关联时出现错误,因此它们似乎相同,但只有第一个有效。该课程给了我错误:
TypeError: __init__() takes at least 4 arguments (1 given)