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.

table.timestamps() should set ON UPDATE CURRENT_TIMESTAMP for updated_ts

See original GitHub issue

Expected: When creating the updated_ts column in table.timestamps(), its DDL should be:

timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Actual: The ON UPDATE CURRENT_TIMESTAMP part is missing - the updated_ts column is never autoupdated.

The syntax I’m describing works for MySQL, I’m not sure how compatible across database engines it is.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:42
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

61reactions
codeclowncommented, Feb 19, 2018

Instead of relying on bookshelf you can create the datetime-fields manually (mysql):

table.dateTime('created_at').notNullable().defaultTo(knex.raw('CURRENT_TIMESTAMP'))
table.dateTime('updated_at').defaultTo(knex.raw('NULL ON UPDATE CURRENT_TIMESTAMP'))

Or if you prefer updated_at to default to creation time as well:

table.dateTime('created_at').notNullable().defaultTo(knex.raw('CURRENT_TIMESTAMP'))
table.dateTime('updated_at').notNullable().defaultTo(knex.raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
59reactions
ericbiewenercommented, Nov 21, 2019

I think timestamps() needs to either provide this auto-update functionality or the docs need to explicitly state that it does not. I simply assumed that functionality was a critical reason for why the timestamps() method exists at all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

11.2.5 Automatic Initialization and Updating for ...
With an ON UPDATE CURRENT_TIMESTAMP clause and a constant DEFAULT clause, the column is automatically updated to the current timestamp and has the...
Read more >
mysql - When is a timestamp (auto) updated?
in it. DEFAULT CURRENT_TIMESTAMP means that any INSERT without an explicit time stamp setting uses the current time. Likewise, ON UPDATE CURRENT_TIMESTAMP means ......
Read more >
Why $table->timestamps() puts 'ON UPDATE ...
I traced the problem and found ON UPDATE CURRENT_TIMESTAMP is set on the created_at column. After I disabled that, the code only update...
Read more >
Automatically updating a timestamp column in PostgreSQL ...
This will create a table with the name user_task with columns created_on, updated_on, and status. Created_on and Updated_on are by default ...
Read more >
Sequelize timestamps option and format explained
When you sync() the above model, the generated CREATE TABLE ... while the updatedAt column will store a timestamp of when the row...
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