Question: How to make the default value of two different datetime fields match?
See original GitHub issueHi all!
How to make the default value of two different datetime fields match?
Say I have the following BaseModel
:
class BaseModel(Model):
id = CharField(primary_key=True, default=lambda: shortuuid.uuid())
created_at = DateTimeTZField(default=lambda: dt.now(tz))
updated_at = DateTimeTZField(default=lambda: dt.now(tz))
Now, when the object is created/instantiated, the values in created_at
and updated_at
will have slightly different timestamps (which is completely understandable).
What would be the ideal approach to make sure they match 100% when an object is first created?
I initially tried using DateTimeTZField(constraints=[SQL('DEFAULT CURRENT_TIMESTAMP')])
instead, but when creating a new record (eg: BaseModel.create()
), Peewee will try to insert created_at
and updated_at
as NULL
, which will override the DB server default.
Then I thought that maybe I could use pre_init
or pre_save
signals to explicitly set instance.updated_at = instance.created_at
, which works, but I thought there might be a better way to accomplish this.
Thank you in advance!
Issue Analytics
- State:
- Created a year ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
I don’t know what evolve is. It’s not part of this library. Database-provided defaults work just fine with peewee, as do python-side defaults.
Sorry @coleifer, I thought peewee db evolve was part of this library. Thanks very much for your help!