Python Forum

Full Version: SQLalchemy issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have been using sqlalchemy both within and without Flask and never had ant issues.
I know that my model is OK as I use it to populate my database.
However when trying to do a simple query, I'm getting improper results.
First I tested a standard select statement using DB Browser:
select CountyId from CountyInfo where StateAbbr = 'IL' and CountyName = 'Bond County';
and it returns the correct result '005'
then I try sqlalchemy.orm query and get wrong value (which happens to be in the first row of the table.
>>> from sqlalchemy.orm import sessionmaker
>>> import IL_Model
>>> tmap = IL_Model
>>> db = tmap.engine
>>> Session = sessionmaker(bind=db)
>>> session = Session()
>>> ci = tmap.CountyInfo
>>> county_id = session.query(ci.CountyId).filter(ci.StateAbbr == 'IL' and ci.CountyName == "Bond County").first()
>>> county_id
('001',)
001 is the first CountyId in the table, result should have been 005

I don't know if I'm just too tired and can't see what's wrong (been up most of the night), but I don't see what I am doing wrong?

I've done this sort of thing numerous times in the past and never had a problem.

Can anyone see my obvious mistake?
not sure if it will make difference, but shouldn't it be filter(ci.StateAbbr == 'IL', ci.CountyName == "Bond County")
That's it!
It accepted my incorrect syntax, and I am indeed too tired.
Thank you!