views:

270

answers:

1

Hello there,

i want to know if sqlalchemy has some problems querying a view? Because if i query the view with normal sql on the server like:

    select * from ViewMyTable where index1 = '608_56_56';

i get a hole butch of entries.

But with SQLAlchemy i get only the first one. But in the count is the correct number. I have no idea why.

This is my SQLAlchemy code.

    myQuery = Session.query(ViewMyTable)
    erg = myQuery.filter(ViewMyTable.index1 == index1.strip())
    totalCount = erg.count() # contains the correct number of all entries i found with that query.
    ergListe = erg.all() # contains only the first entry i found with my query.

Somebody could help me out here please?

Kind regards, Nico

+3  A: 

if you've mapped ViewMyTable, the query will only return rows that have a fully non-NULL primary key. This behavior is specific to versions 0.5 and lower - on 0.6, if any of the columns have a non-NULL in the primary key, the row is turned into an instance. Specify the flag allow_null_pks=True to your mappers to ensure that partial primary keys still count :

mapper(ViewMyTable, myview, allow_null_pks=True)

If OTOH the rows returned have all nulls for the primary key, then SQLAlchemy cannot create an entity since it can't place it into the identity map. You can instead get at the individual columns by querying for them specifically:

for id, index in session.query(ViewMyTable.id, ViewMyTable.index):
    print id, index
zzzeek