MySQL Error 1364 occurs when inserting or updating a row with a NOT NULL column that has no default value and you fail to provide an explicit value. This error is triggered by MySQL's STRICT_TRANS_TABLES mode. Solutions include providing explicit values, adding defaults to columns, using AUTO_INCREMENT, or altering table definitions.
MySQL Error 1364 (ER_NO_DEFAULT_FOR_FIELD) occurs when the database tries to insert or update a record where a column is defined as NOT NULL with no default value, but no explicit value is provided for that column. In strict SQL mode (STRICT_TRANS_TABLES), MySQL rejects the operation to enforce data integrity. This error typically appears in insert statements that don't specify all NOT NULL columns, or in update statements that implicitly leave a column undefined. The error is a safety mechanism—MySQL refuses to guess what value should go into a NOT NULL column, preventing silent data corruption or unexpected NULL values that would violate the schema. The error message indicates which field is problematic, making it straightforward to diagnose. It's more commonly encountered in development than in well-architected production systems, but can occur when schema changes aren't synchronized with application code, or when data migrations are incomplete.
The error message includes the field name directly. Examine the full error output and note which column is causing the issue.
ERROR 1364 (HY000): Field 'username' doesn't have a default valueIn this example, the username column is the problem. Match this to your INSERT or UPDATE statement to understand which columns you need to address.
The most direct solution is to specify all required columns and their values in your INSERT statement.
-- WRONG (missing username):
INSERT INTO users (id, email) VALUES (1, '[email protected]');
-- CORRECT (all NOT NULL columns included):
INSERT INTO users (id, username, email) VALUES (1, 'john_doe', '[email protected]');
-- Or use column order explicitly:
INSERT INTO users (id, username, email, created_at)
VALUES (1, 'john_doe', '[email protected]', NOW());Ensure every NOT NULL column without a default is explicitly listed in the INSERT statement with a corresponding value.
If the column should have a sensible default, modify the table to add one. This prevents future issues and simplifies INSERT statements.
-- Check current column definition:
SHOW COLUMNS FROM users WHERE Field = 'username';
-- Add a default:
ALTER TABLE users MODIFY username VARCHAR(255) NOT NULL DEFAULT 'anonymous';
-- For timestamp columns:
ALTER TABLE users MODIFY created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- For numeric columns:
ALTER TABLE users MODIFY status INT NOT NULL DEFAULT 0;After adding defaults, existing INSERT statements that don't specify these columns will use the default value instead of failing.
If the problematic column is a primary key or unique identifier, configure it as AUTO_INCREMENT.
-- Modify an existing column:
ALTER TABLE users MODIFY user_id INT NOT NULL AUTO_INCREMENT UNIQUE;
-- Or during table creation:
CREATE TABLE users (
user_id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (user_id)
);
-- Now you can insert without specifying user_id:
INSERT INTO users (username, email) VALUES ('john_doe', '[email protected]');AUTO_INCREMENT automatically generates unique sequential values, eliminating the need to provide IDs manually.
If the issue stems from your application, ensure every INSERT or UPDATE includes values for all NOT NULL columns without defaults.
// Node.js / Express example:
// WRONG:
const user = await User.create({ email: '[email protected]' });
// CORRECT:
const user = await User.create({
username: 'john_doe',
email: '[email protected]',
status: 1
});# Python / SQLAlchemy example:
# CORRECT:
user = User(username='john_doe', email='[email protected]', status=1)
db.session.add(user)
db.session.commit()Review database schema documentation and ensure your application provides all required fields during create/update operations.
Inspect the table structure to identify NOT NULL columns and their defaults.
-- View full table structure:
DESC users;
-- or:
SHOW CREATE TABLE users\G
-- Check specific column:
SHOW COLUMNS FROM users WHERE Field = 'username';
-- The "Null" column shows "NO" for NOT NULL
-- The "Default" column shows any default value, or NULL if noneLook for columns where "Null" is "NO" and "Default" is empty—these are the ones that require explicit values in INSERT statements.
For applications using ORMs like Sequelize, Eloquent, or SQLAlchemy, ensure column definitions in your model match the database schema. When you add a NOT NULL column to an existing table via migration, you must provide a default value or the migration will fail on tables with existing data. Use ALTER TABLE with a DEFAULT before removing the default if you want strict enforcement going forward.
In some cases, developers disable STRICT_TRANS_TABLES mode, but this is strongly discouraged in production. Instead, fix the root cause by aligning schema with application logic. The error is there to protect data integrity—respecting it prevents subtle bugs and inconsistent state.
For bulk operations like LOAD DATA INFILE or INSERT SELECT, ensure source and target columns align. Missing columns in the SELECT will fill missing fields with NULL, which fails the NOT NULL check. Use explicit column lists: INSERT INTO target (col1, col2, col3) SELECT col1, col2, col3 FROM source;
When adding new NOT NULL columns to an existing table with data, MySQL requires an explicit DEFAULT or migration fails. Use ALTER TABLE table_name ADD COLUMN new_col INT NOT NULL DEFAULT 0; then optionally remove the DEFAULT afterward with ALTER TABLE table_name MODIFY new_col INT NOT NULL; if strict enforcement is desired.
ERROR 1064: You have an error in your SQL syntax
How to fix "ERROR 1064: You have an error in your SQL syntax" in MySQL
ERROR 1054: Unknown column in field list
Unknown column in field list
ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE (3589): RANGE frame requires numeric ORDER BY expression
RANGE frame requires numeric ORDER BY expression in MySQL window functions
CR_ALREADY_CONNECTED (2058): Handle already connected
How to fix "CR_ALREADY_CONNECTED (2058): Handle already connected" in MySQL
ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name
How to fix ER_WINDOW_DUPLICATE_NAME (3591) in MySQL window functions