I have these data models:
class User(BaseModel, table=True):
id: int
class Car(...):
id: int
class Purchase(...):
id: int
user_id: int
car_id: int
I want to create a query that checks (when committing a purchase); if the relevant user and car exists in MySQL with SQLAlchemy and Python
I have tried this statement:
user_ids= select(User.id)
car_ids = select(Car.id)
statement = select(Purchase).where(and_(
Purchase.user_id.in_(user_ids),
Purchase.car_id.in_(car_ids)
))
records = self._run_select_query(statement)
def _run_select_query(self, statement) -> list:
with Session(self._engine) as session:
results = session.exec(statement)
return list(results.all())
But the returned records object is an empty list
1