A TIMESTAMP column changes on every UPDATE instead of defaulting to NULL
Your error
`shipped_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP2 more lines it can showFix it fast
Most common cause, 1 of 2The column definition already holds the automatic properties
Redefine the column with the behaviour you want.
ALTER TABLE orders MODIFY shipped_at TIMESTAMP NULL DEFAULT NULL;
-- creation time that should never move
ALTER TABLE orders MODIFY created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;Short answer
A TIMESTAMP that refreshes on every UPDATE, or will not stay NULL, carries DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP in its definition. MySQL gives the first TIMESTAMP column both only while explicit_defaults_for_timestamp is OFF; the default is ON. Check SHOW CREATE TABLE, then MODIFY the column to TIMESTAMP NULL DEFAULT NULL. MySQL 8.4 and 9.7.
Which cause do you have?
Print the stored definition of the table and the current value of the setting. The definition shows what the column does today; the setting decides what new tables get.
SHOW CREATE TABLE orders\G
SELECT @@GLOBAL.explicit_defaults_for_timestamp, @@SESSION.explicit_defaults_for_timestamp;Then find the line you see in this list and open its fix:
- 1The column definition already holds the automatic properties
SHOW CREATE TABLE lists DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP on a column you never declared that way, while the setting is now 1 - 2explicit_defaults_for_timestamp is switched off on this server or session
One of the two values in the first step is 0, and newly created tables get the automatic clauses again
What does the error look like?
`shipped_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPThe column was declared as plain TIMESTAMP, yet it holds the time of the last UPDATEInserting NULL into the TIMESTAMP column stores the current time instead of NULL
Cause 1 of 2
The column definition already holds the automatic properties
You seeSHOW CREATE TABLE lists DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP on a column you never declared that way, while the setting is now 1
Check
The timestamp refreshes because ON UPDATE CURRENT_TIMESTAMP is part of the stored column definition. With explicit_defaults_for_timestamp disabled, MySQL gives the first TIMESTAMP column of a table both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP when neither is written. A table created that way, on an older server or by a dump that spelled the clauses out, keeps them after the setting changes.
SHOW CREATE TABLE orders\GFix
Redefine the column with the behaviour you want. For an optional time, permit NULL and default to NULL. For a creation time, keep DEFAULT CURRENT_TIMESTAMP and leave out ON UPDATE.
ALTER TABLE orders MODIFY shipped_at TIMESTAMP NULL DEFAULT NULL;
-- creation time that should never move
ALTER TABLE orders MODIFY created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;Verify
The definition no longer has ON UPDATE, and an UPDATE of another column leaves the timestamp alone.
SHOW CREATE TABLE orders\G
UPDATE orders SET status = 'paid' WHERE id = 42;
SELECT shipped_at FROM orders WHERE id = 42;Confirmed by dev.mysql.com
Cause 2 of 2
explicit_defaults_for_timestamp is switched off on this server or session
You seeOne of the two values in the first step is 0, and newly created tables get the automatic clauses again
Check
When the setting is off, TIMESTAMP columns that are not declared NULL become NOT NULL, and assigning NULL stores the current timestamp. Its default value is ON, and the variable is deprecated, so a 0 comes from an option file line or a SET in the session or tool that creates the tables. MySQL also logs a warning when it is disabled.
sudo grep -rn -i "explicit.defaults.for.timestamp" /etc/mysql/ /etc/my.cnf /etc/my.cnf.d/ 2>/dev/nullFix
Delete the line that turns it off and restart the server, and drop any SET of it from migration scripts. Then fix existing columns as in the first cause.
# remove from the [mysqld] section:
# explicit_defaults_for_timestamp = OFF
sudo systemctl restart mysql # use the service name of your installVerify
Both values read 1, and a new table with a plain TIMESTAMP column shows no automatic clauses.
SELECT @@GLOBAL.explicit_defaults_for_timestamp, @@SESSION.explicit_defaults_for_timestamp;
CREATE TABLE ts_probe (t TIMESTAMP NULL);
SHOW CREATE TABLE ts_probe\G
DROP TABLE ts_probe;Confirmed by dev.mysql.com
How do I stop it coming back?
- Write every TIMESTAMP column with its NULL or NOT NULL attribute and an explicit DEFAULT, so the setting cannot change what the column does.
- Review SHOW CREATE TABLE output in schema diffs after imports; dumps carry the ON UPDATE clauses of the source tables.
- Leave explicit_defaults_for_timestamp at its default and remove it from copied option files.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, QistLock, ShopGo.