abipc
abipc

Reputation: 1035

MySQL created_timestamp more than updated_timestamp

I am using Django ORM and have set auto_now and auto_now_add as follows..

updated_at = models.DateTimeField(auto_now=True, auto_now_add=True, blank=False, null=True, default=None)
created_at = models.DateTimeField(auto_now=False, auto_now_add=True, blank=True, null=True)

I have some records where updated_at is less than created_at by around upto 50 seconds..

NOTE I have only one MySQL master machine and multiple Django servers.. How is it possible to have created_timestamp more than updated_timestamp..

Upvotes: 0

Views: 218

Answers (2)

Rahul Gupta
Rahul Gupta

Reputation: 47876

You are defining both auto_now and auto_now_add in updated_at datetime field which is wrong.

As per Django docs on DateTime field:

The options auto_now_add, auto_now, and default are mutually exclusive. Any combination of these options will result in an error.

It should be something like:

updated_at = models.DateTimeField(auto_now=True, blank=False, null=True, default=None)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True) 

Note:

auto_now: Use this option for setting 'last_modified' timestamps

auto_now_add: Use this option for setting the 'creation' timestamp.

Upvotes: 0

Daniel Hepper
Daniel Hepper

Reputation: 29967

Setting auto_now/auto_now_add fields happens in the application, not in the database (in DateTimeField.pre_save).

You might have some really weird race condition, but the easiest explanation is probably that the clocks on your web servers are not in sync.

Upvotes: 1

Related Questions