0

On my example below, I am using sqlalchemy orm to update the values of a column (column_2) conditional to the values of another column (column_1).

I am currently using two updates to achieve that.

Is it possible to combine these two updates into one? That would save the second execute on my code below.

update_1 = update(table).where(table.c[column_1] != "ABC").values({table.c[column_2]:(table.c[column_3] * table.c[column_4]) })

db.execute(update_1)

update_2 = update(table).where(table.c[column_1] == "ABC").values({table.c[column_2]:table.c[column_3] })

db.execute(update_2)

Stamatis Tiniakos
  • 698
  • 1
  • 11
  • 33
  • 1
    You don't need to use update statements for this. Why don't you make a query, modify the returned objects and commit them? Let SQLAlchemy do his job and create the update statements by his own. – Ramon Dias Jul 29 '22 at 00:13
  • @RamonDias could you provide an example? Not sure what you mean. – Stamatis Tiniakos Jul 29 '22 at 07:07
  • 1
    Check [this out](https://stackoverflow.com/a/26920108/8843585) . When you use the update statements with Table instances, you're getting far from the 'ORM way' and closer to the 'Core way' to do things. – Ramon Dias Jul 29 '22 at 11:59

0 Answers0