9

I wrote a SQL query that I'm trying to port to SQLAlchemy, but receive the following error:

sqlalchemy.exc.InvalidRequestError: Can't join table/selectable 'workflows' to itself

SQL (working):

SELECT
   w.user_id, COUNT(l.id) 
FROM
   logs as l
INNER JOIN
   workflows as w 
   ON l.workflow_id = w.id
WHERE
   l.type = 's'
   AND l.timestamp > extract(epoch from now()) - 86400
GROUP BY
   w.user_id;

SQLAlchemy (not working):

session.query(
   Workflow.user_id, func.count(Log.id)
).join(
   Workflow, Workflow.id == Log.workflow_id
).filter(
   Log.type == 's', Log.timestamp > time.time() - 86400
).group_by(
   Workflow.user_id
).all()

Here is the expected output:

+----------+---------+
|  user_id |  count  |
+----------+---------+
| 1        | 5       |
| 2        | 10      |
+----------+---------+

What am I doing wrong?

2 Answers 2

13

Part

.query(Workflow.user_id, func.count(Log.id))

adds both Workflow and Log to your query. The first model is marked as primary table and others are marked as secondary. If there is no calls to .join() afterwards, both primary and secondary tables will be added to the FROM clause. If there are calls to .join() it will move table it receives to the JOIN clause. The important thing here is that .join() can be applied only to the secondary table.

The problem is that your call to

.join(Workflow, Workflow.id == Log.workflow_id)

tries to mark primary table as joined. To fix issue you need to join secondary table:

.join(Log, Workflow.id == Log.workflow_id)

You can add echo=True to see SQL generated by SQLAlchemy. It's really convenient to debug your queries. Or you can compile single query to see generated SQL.

2
  • Hmm.. I changed the SQLAlchemy to session.query(Workflow.user_id, func.count(Log.id)).filter(Log.type == 's').filter(Log.timestamp > time.time() - 86400).group_by(Workflow.user_id).all(). This was the generated SQL: SELECT workflows.user_id AS workflows_user_id, count(logs.id) AS count_1 FROM workflows, logs WHERE logs.type = %(type_1)s AND logs.timestamp > %(timestamp_1)s GROUP BY workflows.user_id. It returns a count of 2 for both user_ids which is incorrect - it should be 5 for user_id 1 and 10 for user_id 2.
    – okoboko
    Aug 1, 2015 at 23:09
  • Yeah, that's because tables are not joined. As an easy solution you can add Workflow.id == Log.workflow_id into you .where() method. Or try .join(Log, Workflow.id == Log.workflow_id) to configure proper join (not sure weather it works). Aug 1, 2015 at 23:18
2

As noted before, the primary entity of your query is Workflow, and so the join is trying to join Workflow to itself, which is not possible, at least not without using aliases.

In addition to simply reordering the join you can also explicitly control the left hand side of the join using Query.select_from():

session.query(
    Workflow.user_id, func.count(Log.id)
).select_from(
    Log
).join(
    Workflow, Workflow.id == Log.workflow_id
).filter(
    Log.type == 's', Log.timestamp > time.time() - 86400
).group_by(
    Workflow.user_id
).all()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.