SQLAlchemy 过滤器基于自身列的比较
SQLAlchemy filter based on comparison of own columns
我正在寻找基本上等同于 Django 的 SQLAlchemy 的 F() 表达式
https://docs.djangoproject.com/en/3.2/topics/db/queries/#filters-can-reference-fields-on-the-model
我有一个模型:
class MyModel(db.Model):
total = db.Column(db.Integer())
previous_total = db.Column(db.Integer())
我想查询 my_models,其中总计 > previous_total。
在 Django 中看起来像 my_models = MyModel.objects.filter(total__gt=F('previous_total'))
我一直在努力使用 SQLAlchemy 文档,部分原因是大多数文档的结构似乎都倾向于使用 session.query
API 而我正在使用的代码库使用 MyModel.query
检索 total > previous_total
SQLAlchemy 查询
的行
my_query = session.query(MyModel).filter(MyModel.total > MyModel.previous_total)
或者如果您使用的是将会话注入模型的东西 class,比如 flask-sqlalchemy:
my_query = MyModel.filter(MyModel.total > MyModel.previous_total)
我正在寻找基本上等同于 Django 的 SQLAlchemy 的 F() 表达式 https://docs.djangoproject.com/en/3.2/topics/db/queries/#filters-can-reference-fields-on-the-model
我有一个模型:
class MyModel(db.Model):
total = db.Column(db.Integer())
previous_total = db.Column(db.Integer())
我想查询 my_models,其中总计 > previous_total。
在 Django 中看起来像 my_models = MyModel.objects.filter(total__gt=F('previous_total'))
我一直在努力使用 SQLAlchemy 文档,部分原因是大多数文档的结构似乎都倾向于使用 session.query
API 而我正在使用的代码库使用 MyModel.query
检索 total > previous_total
SQLAlchemy 查询
my_query = session.query(MyModel).filter(MyModel.total > MyModel.previous_total)
或者如果您使用的是将会话注入模型的东西 class,比如 flask-sqlalchemy:
my_query = MyModel.filter(MyModel.total > MyModel.previous_total)