Last active
April 14, 2021 12:43
-
-
Save paradoxxxzero/32ccb376d55e72b17eda254a70f03954 to your computer and use it in GitHub Desktop.
sqlalchemy 1.14 sql syntax error for joinedload on relationship with comparison to null and entity with query_expression as null
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey | |
from sqlalchemy.orm import ( | |
declarative_base, | |
joinedload, | |
query_expression, | |
relationship, | |
sessionmaker, | |
) | |
engine = create_engine( | |
"postgresql+psycopg2://test_@localhost/test_query_expr_joinedload" | |
) | |
Base = declarative_base() | |
class Address(Base): | |
__tablename__ = "addresses" | |
id = Column(Integer, primary_key=True) | |
email_address = Column(String, nullable=True) | |
user_id = Column(Integer, ForeignKey("users.id")) | |
class User(Base): | |
__tablename__ = "users" | |
id = Column(Integer, primary_key=True) | |
expr = query_expression() | |
addresses_with_email = relationship( | |
Address, | |
primaryjoin=lambda: (Address.user_id == User.id) | |
& (Address.email_address != None), | |
) | |
Base.metadata.create_all(engine) | |
Session = sessionmaker(bind=engine) | |
session = Session() | |
query = session.query(User).options(joinedload("addresses_with_email")).limit(10) | |
print(query) | |
query.all() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment