Skip to content
Advertisement

How to fix, where condition with update not working

I am trying to update my table using where condition. I am not sure where I am going wrong.

I am using peewee for SQL queries in python. I am trying to update two different tables having the same column. The query is working fine with table1 but not with table2, though they both are same. And the most strange part, there are no errors. I also enabled query logging.

if x:
   for id in x:
       table1.update(status = 0).where(table1.slab_id == id).execute()
       table2.update(status = 0).where(table2.slab_id == id).execute()

expected: table1 and table2 to be updated with status = 0, for provided id. actual: table1 is successfully updated, while all values of table2 are getting updated instead of only one. I also logged the query they are as follows:

('UPDATE `table1` SET `status` = %s WHERE (`table1`.`slab_id` = %s)', [0, 44])
('UPDATE `table1` SET `status` = %s WHERE (`table1`.`slab_id` = %s)', [0, 43])
('UPDATE `table2` SET `status` = %s', [0])
('UPDATE `table2` SET `status` = %s', [0])

'UPDATE `table2` SET `status` = 0 WHERE `table2`.`slab_id` = 43 -> works

For table2 where a condition is not applied, I am failing to understand why?

Advertisement

Answer

This might be the case of version mismatch or foreign key constraint, which does not allow to update or delete a table.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement