Nic*_*ico 5 python postgresql sqlalchemy
我想知道SQLAlchemy是否在查询视图时遇到问题.如果我在服务器上使用普通SQL查询视图,例如:
SELECT * FROM ViewMyTable WHERE index1 = '608_56_56';
Run Code Online (Sandbox Code Playgroud)
我得到了一大堆记录.但是使用SQLAlchemy我只得到第一个.但在计数中是正确的数字.我不知道为什么.
这是我的SQLAlchemy代码.
myQuery = Session.query(ViewMyTable)
erg = myQuery.filter(ViewMyTable.index1 == index1.strip())
# Contains the correct number of all entries I found with that query.
totalCount = erg.count()
# Contains only the first entry I found with my query.
ergListe = erg.all()
Run Code Online (Sandbox Code Playgroud)
如果已映射ViewMyTable,则查询将仅返回具有完全非NULL主键的行.此行为特定于版本0.5及更低版本 - 在0.6上,如果任何列在主键中具有非NULL,则该行将变为实例.指定allow_null_pks=True映射器的标志以确保部分主键仍然计数:
mapper(ViewMyTable, myview, allow_null_pks=True)
Run Code Online (Sandbox Code Playgroud)
如果OTOH返回的行具有主键的所有空值,则SQLAlchemy无法创建实体,因为它无法将其放入标识映射中.您可以通过查询具体来获取各个列:
for id, index in session.query(ViewMyTable.id, ViewMyTable.index):
print id, index
Run Code Online (Sandbox Code Playgroud)