1

Part of my raw sql statement looks like this:

select /*some selects*/
if(/*condition*/, table1.price , if(/*condition*/, t2.price, t3.price)) as price
/*some joins*/
left join table2 t2 on table1.type=t2.id
left join table3 t3 on table1.type=t3.id

This statement works as expected.

SQLAlchemy ORM:

query = db_session.query(Table1,\
                         func.IF(Table1.field5 == 5, Table1.price,\
                         func.IF(Table1.new_model == 1, Table2.price, Table3.price)) 
#+some selects
#+some joins
query = query.join(Table2, Table1.type == Table2.id)\
             .join(table3, Table1.type == Table3.id)

And it doesn`t work the same way. It returns the result that only connected to the Table2. And not using this joins in query returns needed rows, but without needed fields from this Table2 and Table3, of course.

What is my mistake?

4
  • Somewhat off-topic question: Why are you migrating to an ORM? Commented Jan 11, 2016 at 8:11
  • @khajvah, not sure, but mainly because its development standart. But handling many conditions and manual joins is painful, raw sql looks clearer to me. Commented Jan 11, 2016 at 8:23
  • It's not a development "standard" at all. Raw SQL is clearer (this is a personal preference) and much more manageable. Commented Jan 11, 2016 at 8:26
  • 1
    I agree, but job tasks must be done. Commented Jan 11, 2016 at 8:44

2 Answers 2

2

you need to use outerjoin for LEFT JOIN

Sign up to request clarification or add additional context in comments.

Comments

0

LEFT JOIN and JOIN are different operations.

For LEFT JOIN use outerjoin. For JOIN (aka INNER JOIN) use join.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.