Skip to content
Advertisement

SQLAlchemy/SQL: Filter table, then join to query

I need to filter down some results before joining them. I have three models: ModelA, ModelB, and ModelC. ModelA and ModelB both have a relationship to ModelC and they both have an organization_id, which always needs to be filtered down. ModelB doesn’t have an organization_id (the data is shared across all organizations).

I need ModelB joined onto a query that already includes ModelA. The join should only included items where ModelA.organization_id == ModelB.organization_id, ModelB.model_c_id == ModelA.model_c_id, and ModelB is not expired.

My plan is to filter ModelB down by expiration and organization_id, then use a subquery() to join the filtered results to the main query.

I have the following Python code:

This is the resulting SQL from the debugger (without most of the AS statements):

This seems like roughly what I want. All other code is well-tested and working properly when I comment out this code, so it seems that the problem is with the above code. Any ideas what could be wrong?

Advertisement

Answer

It could be a problem with sub = ModelB.subquery(). The code lead to an error, but is also wrong logically because it takes out the constraint for org_id.

I replaced it with sub = sub.subquery() and the code worked for me:

I also received an error for ModelB.objects so that I combined two lines of your code into one. “2” was the example of the requested org_id.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement