Hi, 

I'm trying to build an SQLalchemy ORM query dynamically based on user 
selected options.  Some of these options come from the same table, but the 
user could select either one or both criteria to filter on.  Since I don't 
know which options the user will select ahead of time, I have to join to 
the same table multiple times.  However this throws an error 

ProgrammingError: (psycopg2.ProgrammingError) table name "TableB" specified 
more than once

when I try to submit the query.  How can I find out which tables have 
already been joined in a query?  Or what's the best way to handle building 
a query based on multiple criteria?   I'm using SQLalchemy 1.0.0. 

Here is my pseudo-code.  

Option 1.  Option 2.  Option 3.   (any or all options can be selected, and 
they all come from the same joined table)

// base table
query = session.query(TableA)

// add on new criteria
if option 1: query = query.join(TableB).filter(TableB.option1 > X )
if option 2: query = query.join(TableB).filter(TableB.option2 > X )
if option 3: query = query.join(TableB).filter(TableB.option3 > X )

However, when attempting query.all(), this throws the above error, if I 
have selected any two options.   What I think it should be is something 
like this...

//base
query = session.query(TableA)
//join
query = query.join(TableB)
// add on new criteria
if option 1: query = query.filter(TableB.option1 > X )
if option 2: query = query.filter(TableB.option2 > X )
if option 3: query = query.filter(TableB.option3 > X )

but I don't want to join to TableB if I don't have to.  I have many 
different tables where this kind of situation applies, and it seems 
inefficient to join to all other tables just in case I may need to filter 
on something.  

Any thoughts, help or suggestions?
Thanks, Brian





-- 
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