First Check
Commit to Help
Example Code
import logging
from typing import List
from sqlmodel import (
Field,
Relationship,
SQLModel,
Session,
create_engine,
select,
)
logging.basicConfig(level=logging.INFO)
sqlite_url = "sqlite://"
engine = create_engine(sqlite_url)
# engine = create_engine(sqlite_url, echo=True)
class Product(SQLModel, table=True):
id: str = Field(primary_key=True)
product_status: int = 0
source_status: str = Field(foreign_key="source.source_status")
source: "Source" = Relationship(back_populates="products")
class Source(SQLModel, table=True):
id: int = Field(primary_key=True)
source_status: int = 0
another_attribute: bool = False
products: List["Product"] = Relationship(back_populates="source")
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
def add_data():
with Session(engine) as session:
source_1 = Source(id=1, another_attribute=True)
source_2 = Source(id=2, source_status=2)
product_a = Product(id=1, source=source_1)
product_b = Product(id=2, source=source_1)
product_c = Product(id=3, source=source_2)
product_d = Product(id=4, source=source_2)
product_e = Product(id=5, source=source_2)
session.add(product_a)
session.add(product_b)
session.add(product_c)
session.add(product_d)
session.add(product_e)
session.commit()
def get_data_bad():
with Session(engine) as session:
db_statement = select(Product).where(Product.source.another_attribute == False)
result = session.exec(db_statement).all()
return result
def get_data_bad2():
with Session(engine) as session:
db_statement = select(Product).where(Product.source.source_status == 2)
result = session.exec(db_statement).all()
return result
def get_data_good():
with Session(engine) as session:
db_statement = select(Product).where(Product.source_status == 2)
result = session.exec(db_statement).all()
return result
def main():
create_db_and_tables()
add_data()
try:
bad_data = get_data_bad()
except AttributeError as err:
bad_data = None
logging.error(err)
try:
bad_data2 = get_data_bad2()
except AttributeError as err:
bad_data2 = None
logging.error(err)
good_data = get_data_good()
logging.info(f"BAD DATA: {bad_data}")
logging.info(f"BAD DATA2: {bad_data2}")
logging.info("GOOD DATA:")
for item in good_data:
logging.info(f"{' ' * 4}{repr(item)}")
if __name__ == "__main__":
main()
Description
I am wondering if it is possible to use the where to select based on attributes from a relationship. The example I posted above produces the following output:
ERROR:root:Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Product.source has an attribute 'another_attribute'
ERROR:root:Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Product.source has an attribute 'source_status'
/Users/---/lib/python3.10/site-packages/sqlmodel/orm/session.py:60: SAWarning: Class SelectOfScalar will not make use of SQL compilation caching as it does not set the 'inherit_cache' attribute to ``True``. This can have significant performance implications including some performance degradations in comparison to prior SQLAlchemy versions. Set this attribute to True if this object can make use of the cache key generated by the superclass. Alternatively, this attribute may be set to False which will disable this warning. (Background on this error at: https://sqlalche.me/e/14/cprf)
results = super().execute(
INFO:root:BAD DATA: None
INFO:root:BAD DATA2: None
INFO:root:GOOD DATA:
INFO:root: Product(product_status=0, id='3', source_status='2')
INFO:root: Product(product_status=0, id='4', source_status='2')
INFO:root: Product(product_status=0, id='5', source_status='2')
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.10.2
Additional Context
No response
First Check
Commit to Help
Example Code
Description
I am wondering if it is possible to use the
whereto select based on attributes from a relationship. The example I posted above produces the following output:Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.10.2
Additional Context
No response