Skip to content

Instantly share code, notes, and snippets.

@davidszotten
Created July 4, 2014 16:19
Show Gist options
  • Select an option

  • Save davidszotten/334e56e5125476090983 to your computer and use it in GitHub Desktop.

Select an option

Save davidszotten/334e56e5125476090983 to your computer and use it in GitHub Desktop.
sqlalchemy `and` gotcha
# `and` doesn't work unfortunately.
# python doesn't allow overriding `and` behaviour
>>> print Booking.id == 1 and Booking.id == 2
bookings.id = :id_1
>>> print and_(Booking.id == 1, Booking.id == 2)
bookings.id = :id_1 AND bookings.id = :id_2
print (Booking.id == 1) & (Booking.id == 2)
bookings.id = :id_1 AND bookings.id = :id_2
# & needs parens, has higher priority than ==, unlike `and`
print (Booking.id == 1) and (Booking.id == 2)
bookings.id = :id_1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment