coleifer / peewee

a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb
http://docs.peewee-orm.com/
MIT License
11.06k stars 1.37k forks source link

Hot work "in" #2895

Closed fruitoiz closed 4 months ago

fruitoiz commented 4 months ago

1. User.update(balance=1).where(User.user_id.in_([1, 2]))
2. User.update(balance=1).where(User.user_id << [1, 2])

File "/home/ton/.local/lib/python3.12/site-packages/peewee.py", line 2594, in __sql__
for k, v in sorted(self._update.items(), key=ctx.column_sort_key):
                       ^^^^^^^^^^^^^^^^^^
AttributeError: 'Expression' object has no attribute 'items'

The first and second options don't work. The error is the same.

Tested for: 3.17.5 and 3.15.4 Python: 3.12

coleifer commented 4 months ago

Nonsense bro did you even test this?

from peewee import *

db = SqliteDatabase(':memory:')

class User(db.Model):
    user_id = AutoField()
    balance = IntegerField()

db.create_tables([User])

for i in range(10):
    User.create(balance=i)

User.update(balance=1).where(User.user_id.in_([1, 2])).execute()
User.update(balance=1).where(User.user_id << [3, 4]).execute()
for u in User.select().order_by(User.user_id):
    print(u.user_id, u.balance)

Output is correct:

1 1
2 1
3 1
4 1
5 4
6 5
7 6
8 7
9 8
10 9
fruitoiz commented 4 months ago

Exactly, this happened because I did not indicate what to update.

User.update(User.balance+1).where(User.user_id << [1, 2]) Forgot about balance=