我有三张表:Account
, User
和 Organization
。
Account
包含id
,name
和organization_id
。User
包含email
和organization_id
。Organization
包含id
和name
。
每个 Account
都注册到一个 Organization
(通过 organization_id
),每个 User
也是注册到一个 Organization
。挑战在于显示所有来自与 User
的电子邮件(email),这些 User
是注册到与 Account
相应的 name
的 organization_id
匹配的 Organization
上的。
以下是我的代码:
class Account(db.Model):
__tablename__ = "account"
id = Column(Integer, primary_key=True)
name = Column(String(50), index=True, unique=True)
organization = Column(Integer,
ForeignKey("organization.id"),nullable=False, index=True)
class User(UserMixin, db.Model, RBACUserMixin):
__tablename__ = "user"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
organization = Column(Integer, ForeignKey("organization.id"),
nullable=False, index=True)
class Organization(db.Model):
__tablename__ = "organization"
id = Column(Integer, primary_key=True)
name = Column(String(512))
users = relationship("User", backref="organizations")
accounts = relationship("Account", backref="organizations")