aio-libs / aiopg

aiopg is a library for accessing a PostgreSQL database from the asyncio
http://aiopg.readthedocs.io
BSD 2-Clause "Simplified" License
1.39k stars 159 forks source link

Update "table" with inverse (logic) doesn't work #833

Open leichgardt opened 3 years ago

leichgardt commented 3 years ago

In aiopg, queries to update table fields doesn't work with self inversion. I tried to inverse one field like that:

cmd = 'UPDATE schema.table SET field1 = NOT field1 WHERE field2 = %s'
await cur.execute(cmd, args)

It didn't work. instead of this way, I had to do this in two queries - select and update:

cmd = 'SELECT field1 FROM schema.table WHERE field2 = %s'
res = await cur.execute(cmd, args)
res = not res[0][0]

cmd = 'UPDATE schema.table SET field1 = %s WHERE field2 = %s'
await cur.execute(cmd, (res,) + args)

The syntax for SET field = NOT field is correct and it works in terminal, but I don't know why it doesn't work here.

System

system: ubuntu 20.04 python: 3.8 aiogp: 1.2.1