Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQLalchemy issue
#1
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?
Reply
#2
not sure if it will make difference, but shouldn't it be filter(ci.StateAbbr == 'IL', ci.CountyName == "Bond County")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
That's it!
It accepted my incorrect syntax, and I am indeed too tired.
Thank you!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020