0
我無法理解表達式。如何讓下面的代碼工作?按類型sqlalchemy hybrid_property和表達式
class OperationType(Enum):
MINUS = 1
MINUS_CORR = 2
PLUS = 3
PLUS_CORR = 4
組操作
BALANCE_PLUS_OPERATIONS = [
OperationType.PLUS.value,
OperationType.PLUS_CORR.value
]
BALANCE_MINUS_OPERATIONS = [
OperationType.MINUS.value,
OperationType.MINUS_CORR.value
]
操作模型
class Operation(Model):
__tablename__ = 'operation'
id = db.Column(db.BigInteger, primary_key=True)
created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
operation_type = db.Column(db.SmallInteger, nullable=False)
amount = Column(db.Integer, nullable=False)
user_id = db.Column(db.ForeignKey('users.id'), nullable=False)
user = relationship('User', backref='operation', uselist=False)
用戶模型
class User(UserMixin, Model):
__tablename__ = 'users'
id = Column(db.Integer, primary_key=True)
operations = relationship("Operation", backref="users")
@hybrid_property
def balance(self):
plus = sum(op.amount for op in self.operations if op.operation_type in BALANCE_PLUS_OPERATIONS)
minus = sum(op.amount for op in self.operations if op.operation_type in BALANCE_MINUS_OPERATIONS)
return plus - minus
@balance.expression
def balance(cls):
p = select([func.sum(Operation.amount).label('BALANCE_PLUS_OPERATIONS')]) \
.where(Operation.operation_type.in_(BALANCE_PLUS_OPERATIONS)) \
.where(User.id == cls.id) \
.as_scalar()
m = select([func.sum(Operation.amount).label('BALANCE_MINUS_OPERATIONS')]) \
.where(Operation.operation_type.in_(BALANCE_MINUS_OPERATIONS)) \
.where(User.id == cls.id) \
.as_scalar()
return select([p - m]).label('BALANCE')
表達是錯誤的,會產生錯誤的結果:
users = User.query.filter_by(balance=51).all()
for u in users:
print(u, u.balance)
打印:
<User([email protected])> 51
<User([email protected])> 0
,但我預計只有一條記錄:
<User([email protected])> 51
感謝
請編輯您的問題解釋(1)您預期發生,(2)你得到了什麼錯誤(或者怎樣的結果不同)和(3)縮小問題直到只是有問題的代碼。閱讀[如何創建最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)將有所幫助。 – HFBrowning
謝謝。更正 – 13akaEagle