question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: How to make the default value of two different datetime fields match?

See original GitHub issue

Hi 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:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, Jun 7, 2022

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.

0reactions
jonathanmachcommented, Jun 8, 2022

Sorry @coleifer, I thought peewee db evolve was part of this library. Thanks very much for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compare Nullable DateTime variable for default value
You can create a const: static readonly DateTime DefaultDateTime = new DateTime(1900, 1, 1, 0, 0, 0);. and then compare this way: DateTime?...
Read more >
Set Default Value in a Formula Date/Time Field
DATETIMEVALUE (DATE(year(DATEVALUE(CreatedDate)), month(DATEVALUE(CreatedDate)), day(DATEVALUE(CreatedDate)))) + 7 + 11.50/24.
Read more >
Set default values for fields or controls - Microsoft Support
Set a default value​​ Select the field that you want to change. On the General tab, type a value in the Default Value...
Read more >
How To Convert a String to a datetime or time Object in Python
The following example converts a date and time string into a datetime.datetime() object and prints the class name and value of the resulting ......
Read more >
Creating parameter defaults in Amazon QuickSight
Create a default value for a parameter in Amazon QuickSight. ... the fields in your dataset have names that closely match the field...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found