我试图在两个表之间建立关系,即帐户和 项目:
class Account(Base):
__tablename__ = 'accounts'
account_id = Column(Integer, primary_key=True)
item_order = Column(ARRAY(Integer))
items = relationship(?)
class Item(Base):
item_id = Column(Integer, primary_key=True)
account_id = Column(Integer, ForeignKey('accounts.account_id'))
当我获得一个帐户的所有项目时,他们需要整理好。订单存储在" item_order"在帐户上,否则假定按item_id排序。
以下是我查询帐户所有项目的方法:
SELECT * FROM accounts A INNER JOIN items T USING (account_id)
WHERE A.account_id=1
ORDER BY
CASE WHEN A.item_order IS NOT NULL THEN nullif(idx(A.item_order, T.item_id::int), 0)
ELSE
T.item_id
END
NULLS LAST;
是否可以将其转换为SQLAlchemy关系?