我有這樣定義的聲明表:SQLAlchemy的自定義查詢列
class Transaction(Base):
__tablename__ = "transactions"
id = Column(Integer, primary_key=True)
account_id = Column(Integer)
transfer_account_id = Column(Integer)
amount = Column(Numeric(12, 2))
...
查詢應該是:
SELECT id, (CASE WHEN transfer_account_id=1 THEN -amount ELSE amount) AS amount
FROM transactions
WHERE account_id = 1 OR transfer_account_id = 1
我的代碼是:
query = Transaction.query.filter_by(account_id=1, transfer_account_id=1)
query = query.add_column(case(...).label("amount"))
但它不」請替換amount
列。
一直試圖做幾個小時,我不想使用原始SQL。
排序怎麼樣?我可以拋出一個order_by()嗎? – 2010-03-16 15:57:13
與查詢 - 當然;使用@property - 在數據庫上執行查詢時並不是真的,但是當您檢索並存儲在列表中時,您可以輕鬆地對Transaction對象進行排序。 – van 2010-03-16 16:27:57