Reputation: 2476
I'm having one of those days.
I want to check if a particular row exists in my DB, and if it does, return True. If not, return False.
A brief example:
contact_object = Contact.query.filter(Contact.user_id == user_id and Contact.contact_id == contact_id).first()
This should return exactly 1 row (which it does). Here's what I would like:
if contact_object:
print "YEY, it was found"
else:
print "Nope, not found"
However, contact_object always returns True
, and I always get a "YEY, it was found"
(Note that user_id
and contact_id
are variables that I have defined previously.)
How do I check for a row not found versus a row found? I read the relevant documentation to no avail...but I feel like I'm surely missing something simple?
Thanks for your help.
Upvotes: 2
Views: 6290
Reputation: 8270
I don't believe you can use and
in a filter expression in SQL Alchemy that way:
http://docs.sqlalchemy.org/en/rel_0_8/orm/tutorial.html#common-filter-operators
contact_object = Contact.query \
.filter(Contact.user_id == user_id) \
.filter(Contact.contact_id == contact_id) \
.first()
Upvotes: 3