What the message looks like
ERROR 1067 (42000): Invalid default value for 'created_at'Common causes
- A
TIMESTAMPorDATETIMEcolumn defaults to'0000-00-00 00:00:00'whileNO_ZERO_DATEis on. - The dump comes from MySQL 5.6 and uses defaults that 5.7/8.0 reject under
STRICT_TRANS_TABLES. - The default does not match the type, for example text on a numeric column or a default on
TEXTbefore MySQL 8.0.13. - The character set or collation of the default does not match the column, common in old dumps.
- A migration puts
CURRENT_TIMESTAMPon a column that does not allow it in this version.
Check
- Show the active sql_mode
mysql -e "SELECT @@sql_mode;" - Inspect the column definition
mysqldump --no-data db table | grep -i -A3 -E 'timestamp|datetime' - Compare source and target version
mysql --version; grep -i -m1 'server version' dump.sql
Fix
- Change the default to match the type:
DEFAULT CURRENT_TIMESTAMPinstead of a zero date, or allowNULL. - For an import, relax the mode for the session only:
SET SESSION sql_mode='NO_ENGINE_SUBSTITUTION';– deliberately and not permanently. - Better: test the dump on the target version and regenerate it with suitable options instead of bending the mode.
- Check
sql_modeagain after the import, otherwise zero dates slip in silently.
Typical scenario
A dump from an old application is imported into a new MySQL server and aborts at created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00' with ERROR 1067 (42000): Invalid default value for 'created_at'. The old server allowed zero dates, the new one rejects them through its sql_mode. First check which columns in the dump file use this zero date as a default.
FAQ
Why does this only appear after a server upgrade?
Because STRICT_TRANS_TABLES and NO_ZERO_DATE are on by default in newer versions.
Can I turn sql_mode off permanently?
Technically yes, but then the database silently stores wrong values. Only useful temporarily during an import.
What do I use instead of 0000-00-00?
Use NULL with a nullable column, CURRENT_TIMESTAMP or a valid date. For TIMESTAMP, the smallest valid value is 1970-01-01 00:00:01 UTC. For missing data NULL is the cleanest choice, because it says that no date exists.
How do I still load an old dump?
Relax sql_mode only for the import session, for example with SET SESSION sql_mode=''; before the import, or edit the dump file. Afterwards clean up the column definitions instead of loosening the server permanently, so that future imports run cleanly.
How do I see the active sql_mode?
With SELECT @@GLOBAL.sql_mode, @@SESSION.sql_mode; you see the global and current value. The global value comes from the configuration file or the version's default. The two can differ if the application sets its own mode when connecting.