如何让flask-sqlalchemy的查询对象选择列(或说字段)?
我遇到了一些困扰我一整天的问题。我正在使用flask/flask-sqlalchemy/postgresql,我想这样做:
published_sites = msg_published.query \
.filter( and_(*filter_clause) ) \
.group_by( msg_published.site_id ) \
.order_by( order_clause ) \
.paginate( page_no, per_page, error_out = False )
但在mysql中,这是可以的,而在postgresql中,它是错误的,并在group by子句或a中要求除site_id之外的其他字段聚合函数,我知道 postgresql 对 SQL 比 mysql 更严格,所以我必须在 msg_published 的查询对象中选择 site_id ,但在纯 sqlalchemy 中我可以这样做
published_sites = session.query( msg_published.site_id ) \
.filter( and_(*filter_clause) ) \
.group_by( msg_published.site_id ) \
.order_by( order_clause ) \
.paginate( page_no, per_page, error_out = False )
: Flask-sqlalchemy,如何让它工作?
I met some problem that wired me the whole day. I'm using flask/flask-sqlalchemy/postgresql, and I want to do this:
published_sites = msg_published.query \
.filter( and_(*filter_clause) ) \
.group_by( msg_published.site_id ) \
.order_by( order_clause ) \
.paginate( page_no, per_page, error_out = False )
but in mysql, it is OK, and in postgresql it is wrong and ask for the other fields besides site_id either in a group by clause or in a aggregation function, I know that postgresql is stricter on SQL than mysql , so I must select the site_id in the query object of msg_published, but in pure sqlalchemy I can do like this:
published_sites = session.query( msg_published.site_id ) \
.filter( and_(*filter_clause) ) \
.group_by( msg_published.site_id ) \
.order_by( order_clause ) \
.paginate( page_no, per_page, error_out = False )
and in flask-sqlalchemy, how to get it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你大部分时间都到了那里。要在 PostgreSQL 中执行 MySQL 允许的操作,需要子选择。
You're most of the way there. to do in PostgreSQL what MySQL allows requires a subselect.