I have a class Item
whose id
is a primary key and auto-generated. Now I read data from some other external source, create an Item
object, and need to check if this object is already present in my items
table. How do I do it?
The most efficient way is to use exists()
q = session.query(Item.id).filter(Item.email==email)
session.query(q.exists()).scalar() # returns True or False
You could query for items that have the same attributes and check if the count is greater than zero.
if session.query(Item.id).filter(Item.email==newItem.email,
Item.type==newItem.type).count() > 0:
// item exists
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With