MySQL error 1364: Field 'id' doesn't have a default value on INSERT
ERROR 1364 (HY000): Field 'id' doesn't have a default valueShort answer
Error 1364 means the INSERT left out a NOT NULL column that has no default, and strict SQL mode (the default) refuses to guess. For an id column the usual fix is to make it AUTO_INCREMENT. Otherwise supply the value in the INSERT, or change the mapping so it does.
Fix it fast
1Find out which cause you have
Look at how the id column is defined and which SQL mode is active.
SHOW CREATE TABLE tbl_name\G SELECT @@SESSION.sql_mode;2Match what you see, then jump to its fix
- 1
SHOW CREATE TABLE shows id NOT NULL with no AUTO_INCREMENTThe id column is not AUTO_INCREMENTFix - 2
The column named in the message is not the id: the error names whichever column was left outStrict SQL mode is on and a NOT NULL column is simply missing from the INSERTFix - 3
The error comes from a JPA save() callHibernate is told to generate ids, but the table cannot do itFix
- 1
What does the error look like?
ERROR 1364 (HY000): Field 'id' doesn't have a default valueField '%s' doesn't have a default value (the server's message pattern; %s is the column name)
Cause 1 of 3
The id column is not AUTO_INCREMENT
1Check
MySQL generates the id when you omit it only if the column is AUTO_INCREMENT. A missing value is a NOT NULL column with no explicit DEFAULT, and strict mode turns that into an error.
SHOW CREATE TABLE tbl_name\G2Fix
Restate the full definition with AUTO_INCREMENT. MODIFY drops attributes you do not repeat, so copy the type and other attributes from the output above (the type here is an example).
ALTER TABLE tbl_name MODIFY id BIGINT NOT NULL AUTO_INCREMENT;3Verify
Insert a row without id; MySQL assigns the next number.
INSERT INTO tbl_name (name) VALUES ('test');
SELECT LAST_INSERT_ID();Confirmed by dev.mysql.com
Cause 2 of 3
Strict SQL mode is on and a NOT NULL column is simply missing from the INSERT
1Check
The default SQL mode in MySQL 8.4 includes STRICT_TRANS_TABLES, so an INSERT that omits a NOT NULL column without a default is an error. Without strict mode MySQL would insert the implicit default and only warn.
SELECT @@GLOBAL.sql_mode, @@SESSION.sql_mode;2Fix
The correct fix is to give the column a value or a DEFAULT in the table. Turning strict mode off hides data errors, so use it only as a stopgap.
ALTER TABLE tbl_name MODIFY status VARCHAR(20) NOT NULL DEFAULT 'new';3Verify
Repeat the INSERT.
INSERT INTO tbl_name (name) VALUES ('test');Confirmed by dev.mysql.com
Cause 3 of 3
Hibernate is told to generate ids, but the table cannot do it
1Check
Hibernate's IDENTITY strategy maps the id to an identity or auto_increment column, so the column must really be AUTO_INCREMENT. The AUTO strategy selects SEQUENCE, TABLE or UUID by id type and database, so it does not guarantee an auto_increment column.
// entity
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;2Fix
Use IDENTITY for a MySQL auto_increment id, and fix the table with the ALTER above if it was created by hand.
ALTER TABLE tbl_name MODIFY id BIGINT NOT NULL AUTO_INCREMENT;3Verify
Call save() on a new entity and read the generated id.
SELECT id FROM tbl_name ORDER BY id DESC LIMIT 1;Confirmed by docs.hibernate.org
How do I stop it coming back?
- Define every id as AUTO_INCREMENT (or a generated identity) in the migration that creates the table.
- Keep strict mode on in every environment, so the error shows in development and not only in production.
- Let migrations create the schema instead of editing tables by hand.
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 built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Databases
Hibernate fails on a CREATE or ALTER TABLE statement at startup
Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement
Hibernate generated a CREATE, ALTER or DROP statement and MySQL refused it.
3 causes, each with a fixDatabases
Hibernate cannot resolve org.hibernate.dialect.MySQL5Dialect as a strategy
Unable to resolve name [org.hibernate.dialect.MySQL5Dialect] as strategy [org.hibernate.dialect.Dialect]
Hibernate 6 stopped using version-specific dialects such as MySQL5Dialect.
3 causes, each with a fixDatabases
Spring Boot fails on startup: HikariPool-1 exception during pool initialization
Caused by: ... (the underlying driver exception)
HikariCP opens one connection when the pool starts, and the startup fails when that connection fails.
3 causes, each with a fixDatabases
Java cannot log in to MySQL 8: authentication plugin caching_sha2_password not supported
ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
MySQL 8 accounts use the caching_sha2_password plugin by default, and Connector/J releases before 8.0.9 cannot use it.
3 causes, each with a fixDatabases
Spring Boot does not create the schema on MySQL or PostgreSQL
No CREATE TABLE statements from Hibernate appear at startup
Spring Boot only defaults ddl-auto to create-drop for an embedded database with no Flyway or Liquibase.
3 causes, each with a fixDatabases
Spring Boot in a container cannot connect to the MySQL container
Communications link failure
In Compose the app and MySQL are separate containers.
3 causes, each with a fix