Can't filter on Join?

34 views
Skip to first unread message

Michael P. McDonnell

unread,
Aug 12, 2019, 5:49:06 PM8/12/19
to sqlalchemy
Hey team -

I'm trying to figure out how to basically rewrite this:
SELECT
count(task.id
FROM task
JOIN round on task.game_id = round.game_id
JOIN tournament ON round.tournament_id = tournament.id
WHERE tournament.id = '626aeaa7-783b-415c-85f9-5222d9c95973';

As this:
total_tasks = column_property(
        select([func.count(Task.id)])
        .outerjoin(Round, Task.game_id == Round.game_id)
        .filter(round.tournament_id == tournament_id)
        .correlate_except(Task))

I keep getting the following error:
AttributeError: 'Join' object has no attribute 'filter'

Which I *know* tells me that a join object has no method called "filter", but there's also no "where", "filter_by" etc...

What painfully obvious thing am I missing?

Mike Bayer

unread,
Aug 12, 2019, 9:44:26 PM8/12/19
to noreply-spamdigest via sqlalchemy
hi -

you're doing a classic mistake-ish thing that will be less possible in 1.4 and the longer term plan is by 2.0 all confusion will be eliminated, which is that you are using the Core select() construct in the way you would use the ORM Query object, when in fact these are two totally different objects that work very differently.

To do a join (and filter) with Core select():

from sqlalchemy import outerjoin, select
j = outerjoin(Task, Round, Task.game_id== Round.game_id)
stmt = select([func.count(..)]).select_from(j).where(...).correlate_except(...)

basically:

1. do not call select.join() or select.outerjoin(). These methods are 100% useless, unfortunately, and are gone in 1.4.      use select.select_from(join(A, B, onclause)).

2. select() doesn't have filter(), it has where().  


For now, as we are still in SQLAlchemy medieval times, background on how to use select() is at https://docs.sqlalchemy.org/en/13/core/tutorial.html#selecting



--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Michael P. McDonnell

unread,
Aug 14, 2019, 11:36:52 AM8/14/19
to sqlal...@googlegroups.com
I'm sorry this took me so long - but thank you. This is actually much cleaner syntax and I like it. 

Thanks for all you do!

Reply all
Reply to author
Forward
0 new messages