MySQL & MariaDB error 1364: Field doesn't have a default value
Field 'name' doesn't have a default value (5.6.51) An INSERT left out a NOT NULL column that has no DEFAULT. In strict mode the row is rejected; without strict mode an implicit default ('' or 0) is used with a warning.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_NO_DEFAULT_FOR_FIELD | ER_NO_DEFAULT_FOR_FIELD |
| SQLSTATE | HY000 | HY000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
Field 'name' doesn't have a default value. Every column you don’t list in an INSERT gets its default. A NOT NULL column without a DEFAULT has none, so strict mode refuses the row. Without strict mode the server invents one (empty string, 0, or the zero date) and only warns, which is the behaviour code written for MySQL 5.6 often relied on.
Why it happens
- Applications migrated from MySQL 5.6 (non-strict by default) to MySQL 5.7+ or MariaDB, which are strict by default.
- A new NOT NULL column was added to a table but the application doesn’t fill it yet.
AUTO_INCREMENTwas dropped from an id column during a migration.
How to fix it
- Give the column a default:
ALTER TABLE p ALTER name SET DEFAULT 'unknown';(MySQL 8.0.13+ also allows expression defaults likeDEFAULT (UUID())). - Or make it nullable, or have the application always supply it.
- Avoid switching strict mode off globally: it hides this and other data problems such as 1406 and 1292.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE p (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20) NOT NULL);Run:
INSERT INTO p () VALUES ();
You get: Field 'name' doesn't have a default value(11.8.9)
Verified fixes
Add a default to the column (or always supply it)
ALTER TABLE p ALTER name SET DEFAULT 'unknown'; INSERT INTO p () VALUES (); SELECT name FROM p;
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Omit a NOT NULL column that has no default (strict mode) repro | ≠ | ✓ 1364 | ✓ 1364 | ✓ 1364 | ✓ 1364 | ✓ 1364 | ✓ 1364 | ✓ 1364 | ✓ 1364 |
| Without strict mode an implicit default ('' for strings) is used with warning 1364 version check | ⚠ 1364 | ⚠ 3090,1364 | ⚠ 1364 | ⚠ 1364 | ⚠ 1364 | ⚠ 1364 | ⚠ 1364 | ⚠ 1364 | ⚠ 1364 |
| Add a default to the column (or always supply it) fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
What the warnings and failures say (3)
- Omit a NOT NULL column that has no default (strict mode) (MySQL 5.6): expected error 1364 did not occur
- Without strict mode an implicit default ('' for strings) is used with warning 1364 (MySQL 5.6, MySQL 8.0, MySQL 8.4, MySQL 9, MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): warning 1364: Field 'name' doesn't have a default value
- Without strict mode an implicit default ('' for strings) is used with warning 1364 (MySQL 5.7): warning 3090: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.; warning 1364: Field 'name' doesn't have a default value