我有以下模型
class Deals(db.Model):
__tablename__ = 'deals'
id = db.Column(db.Unicode(32), primary_key=True)
contract_length = db.Column(db.Integer)
device_price = db.Column(db.Numeric(precision=6, scale=2))
monthly_price = db.Column(db.Numeric(precision=6, scale=2))
device_id = db.Column(db.Integer(), db.ForeignKey('devices.id'))
device_color_id = db.Column(db.Integer)
有一次我跑
results = Deals.query.join(Phone, Deals.device).group_by(Deals.contract_length, Deals.device_price, Deals.monthly_price, Deals.device_id)
deals_p = results.paginate(page, per_page=limit)
分组的原因是我们对不同行(交易ID)的不同设备变体(颜色)分布有相同的交易。 我需要做的是为结果添加其他变体。这可以通过在循环中执行下面的sql并为每个合同添加额外的属性来实现。
Select c.*
From deals as d
Join deals as d1
On d.contract_length = d1.contract_length
And d.device_price = d1.device_price
And d.monthly_price = d1.monthly_price
And d.device_id = d1.device_id
And c.device_color_id <> c1.device_color_id
Where c1.id = ::ID
如何在烧瓶中实现这一点,sqlalchemy?