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

[Question] Block the possibility to change the default value #2879

Closed daeeros closed 4 months ago

daeeros commented 4 months ago

At some point I noticed that the standard json value of the cooldowns attribute is changing, I must have made a mistake somewhere, so the question is, is it possible to block the change of the standard value? And to track it, at what point it happens, would be glad to have an example thanks <3

import peewee as pw
import playhouse.postgres_ext as pw_ex
import time

class BaseModel(pw.Model):
    class Meta:
        database = database

class Player(BaseModel):
    user_id = pw.BigIntegerField(primary_key=True, unique=True)
    cooldowns = pw_ex.BinaryJSONField(default={
        "bonus": 0,
        "safe": 0,
        "bio": 0,
        "dinamite": 0,
        "transfer": 0,
        "rpcgame": 0,
        "double_reward": 0,
    })
    created = pw.BigIntegerField(default=time.time)
coleifer commented 4 months ago

You should never, as a rule, assign a mutable value to a default. Instead, if you intend to use a mutable value, use a callable:


cooldowns = BinaryJSONField(default=lambda: {'bonus': 0, ...})