2
我在SQLAlchemy的核心加入3表,並選擇所有列如下:從多張表中選擇列在SQLAlchemy中加入核心
rows = self.db.execute(self.execs.join(
self.orders.join(self.instruments)
).select(whereClause)).reduce_columns())
它運作良好,但如果我要選擇列的子集:
reqdCols = [order.c.id, exec.c.last_modified, instruments.type]
rows = self.db.execute(self.execs.join(
self.orders.join(self.instruments)
).select(reqdCols, whereClause)).reduce_columns())
它不工作,並給出以下錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/apps/qtdist/pkgs/pub/cpython/python-2.7.6/lib/python2.7/site-packages/sqlalchemy/sql/selectable.py", line 807, in select
return Select(collist, whereclause, from_obj=[self], **kwargs)
File "/apps/qtdist/pkgs/pub/cpython/python-2.7.6/lib/python2.7/site-packages/sqlalchemy/sql/selectable.py", line 2219, in __init__
whereclause).self_group(against=operators._asbool)
File "/apps/qtdist/pkgs/pub/cpython/python-2.7.6/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 3438, in _literal_as_text
"SQL expression object or string expected."
sqlalchemy.exc.ArgumentError: SQL expression object or string expected.
替代方法是使用select研究所ead Join.select並使其隱式加入where子句:
joinConditions = (orders.c.colx == execs.colx) & (execs.c.coly == instruments.c.coly)
select(reqdCols).where(and_(whereClause, joinConditions)
但是我寧願爲了性能原因而隱式地加入隱式。有什麼方法可以使用顯式連接來選擇列的子集?
請你充分回溯? – Nilesh 2014-09-04 03:43:25
whereClause'初始化在哪裏? – Nilesh 2014-09-04 03:47:29
我簡化了它。在實際代碼中,Cuseuse實際上是一個子句的元組,並被稱爲:select(和_(* whereClauses))。另外stacktrace是從python的REPL沒有where子句,因爲我第一次嘗試從解釋器。 – apoorvkul 2014-09-04 03:51:24