Python SQLAlchemy子查询获取AttributeError:';查询';对象没有属性';is#u子句#u元素';
我在从主查询函数进行子查询时遇到问题。我的查询是这样一个类中的函数Python SQLAlchemy子查询获取AttributeError:';查询';对象没有属性';is#u子句#u元素';,python,python-3.x,sqlalchemy,Python,Python 3.x,Sqlalchemy,我在从主查询函数进行子查询时遇到问题。我的查询是这样一个类中的函数 class MyClass(): ... ... @property def main_query(self): main_query = session.query(MainTable) .join(otherTable) .filter(otherTable.id =
class MyClass():
...
...
@property
def main_query(self):
main_query = session.query(MainTable)
.join(otherTable)
.filter(otherTable.id = self.id)
return main_query
出于某种原因,当我尝试在另一个模块中执行以下简单操作时:
q = MyClass.main_query(111) # 111 is the id here
print(str(q)) # I can see this print, it prints the correct query statement
subquery_maxdate = session.query(func.max(Table1.date).label("max_date")).subquery()
query = DBSession.query(q, subquery_maxdate) #This gives me an error
AttributeError: 'Query' object has no attribute 'is_clause_element'
根据用户下拉选择,我正在尝试从主\u查询
分支子查询
似乎我无法从类中运行另一个查询,因为当我只是简单地尝试以下操作时:
q = DBSession.query(q) # This already gives me the error
AttributeError: 'Query' object has no attribute 'is_clause_element'
有人能帮我一下吗。你可以参考我的错误: AttributeError:“ForeignKey”对象没有属性 “is_子句_元素” 我发现错误在班级模型中 我将db.ForeignKey('Model.user\u id')更改为ForeignKey=('Model.user\u id),然后它就解决了
我在一个类似的设置中遇到了同样的问题,但还没有解决。有点沮丧,这个问题没有更详细的答案:(我知道这已经晚了,但可能是因为
main\u query
的返回是query
对象,而不是子查询。可能在过滤器之后添加了.subquery()
(otherTable.id…
修复了该问题。
class User(UserMixin, db.Model):
...
...
message = db.relationship('Message', foreign_keys='Message.user_id')
#note that I use db.relationship NOT db.Column