On Wed, Oct 22, 2014 at 7:02 PM, Michael Bayer <mike...@zzzcomputing.com> wrote:
> Yeah i think that is what you have to do, you want UNION but you want all the 
> criteria generated on the inside.  There’s two approaches to take here, 
> either start with the UNION that polymorphic gives you, the rewrite it:
>
> SELECT u.* FROM (SELECT * FROM a UNION SELECT * FROM b) AS u WHERE u.x = y
>
> becomes ->  SELECT * FROM a WHERE a.x=y UNION SELECT * FROM b WHERE b.x=y
>
> or you can catch each SELECT as they are created earlier, before you build 
> the UNION.   The first approach might be a little harder to implement but it 
> might be more robust.

That's a great idea, but it looks like more work than I have time
available now. I'll definitely try to do something like that later.

> SQLAlchemy does things like this all the time but they are not trivial to 
> implement as you need to think about the whole expression.  The visitor 
> system in sqlalchemy.sql.visitors forms the basis for how operations like 
> this are usually done, providing an interface used to scan and rebuild 
> expressions.     Whole-query rebuilds like that can be tricky, we for example 
> do one against SQLite when we have a SELECT that has nested JOINs (see 
> http://docs.sqlalchemy.org/en/rel_0_9/changelog/migration_09.html#many-join-and-left-outer-join-expressions-will-no-longer-be-wrapped-in-select-from-as-anon-1,
>  the code that does this is at 
> https://bitbucket.org/zzzeek/sqlalchemy/src/56d5732fbdf09508784df6dc4c04e5b39ac6be85/lib/sqlalchemy/sql/compiler.py?at=master#cl-1330.

That's really helpful for me right now. I'm using the cloning visitor
to rebuild the expressions from the recorded filter() calls for the
secondary models and it works like a charm.

Thanks for the help.

-- 
---
Pedro Werneck

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to