我希望只有在没有user = User.query.filter_by(deleted=0).get(id)
的情况下才能通过ID获取用户。我试过了
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/david/.virtualenvs/flask-sqlalchemy/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 831, in get
return self._get_impl(ident, loading.load_on_ident)
File "/home/david/.virtualenvs/flask-sqlalchemy/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 864, in _get_impl
return fallback_fn(self, key)
File "/home/david/.virtualenvs/flask-sqlalchemy/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 177, in load_on_ident
q._get_condition()
File "/home/david/.virtualenvs/flask-sqlalchemy/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 350, in _get_condition
"get", order_by=False, distinct=False)
File "/home/david/.virtualenvs/flask-sqlalchemy/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 368, in _no_criterion_condition
self._no_criterion_assertion(meth, order_by, distinct)
File "/home/david/.virtualenvs/flask-sqlalchemy/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 365, in _no_criterion_assertion
"Query with existing criterion. " % meth)
sqlalchemy.exc.InvalidRequestError: Query.get() being called on a Query with existing criterion.
但收到了错误。如何过滤查询并按ID获取?
int position = getAdapterPosition();
答案 0 :(得分:2)
正如错误所述,get
仅在您未过滤查询时才有效。如果您要进行过滤,请按id
进行过滤,然后使用first
。
user = User.query.filter_by(id=id, deleted=0).first()