I have faced a problem where I have to find if the data exists in the table using SqlAlchemy Core.

I think the best way to do this query is to use exists method, which stops searching as soon as the first item is found. So, I crafted this version of query:

conn = self.db.connect()
query = exists().where(cookie_table.c.cookie_id == cookie_id)
result = conn.execute(query)

But it produces this error:

StatementError: Not an executable clause (original cause: ArgumentError: Not an
executable clause) 'EXISTS (SELECT * \nFROM cookie \nWHERE cookie.cookie_id = ?)' []

I tried modifying it a bit (combining with select), but to no avail.

So, eventually I came up with another solution, using limit(1), which works.

conn = self.db.connect()
query = select([1], cookie_table.c.cookie_id == cookie_id).limit(1)
result = conn.execute(query).fetchone()
return True if result is not None else False

I have two questions:

How the task can be achieved with exists method?

Is the query with limit as efficient as with exists?

Edit
Report