Skip to content

Instantly share code, notes, and snippets.

@pawl
Created March 18, 2017 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawl/893c185519823b76150faa6a4bf74be5 to your computer and use it in GitHub Desktop.
Save pawl/893c185519823b76150faa6a4bf74be5 to your computer and use it in GitHub Desktop.
sqlalchemy empty list in_() issue
# demonstrates the issue fixed in: https://bitbucket.org/zzzeek/sqlalchemy/issues/3907
from sqlalchemy import create_engine, Column, Integer
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://root@localhost/test?charset=utf8mb4',
convert_unicode=True,
echo=True)
session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = session.query_property()
class Post(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True)
#Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
# create new rows if database is empty
for x in range(10):
session.add(Post())
session.commit()
# empty lists trigger insane queries before 1.2.0
Post.query.filter(Post.id.in_([])).all()
"""
SELECT posts.id AS posts_id
FROM posts
WHERE posts.id != posts.id
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment