MySQL error 1048 (Column Cannot Be Null) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Identify the column from the message and check why it is null in your data. If that does not apply, provide a value in the INSERT/UPDATE, or fix the application so the field is always set — the full checklist is below.
MySQL Error Code: 1048
Official name: Column Cannot Be Null
System name: Column cannot be null
Service: MySQL
Column '%s' cannot be null
An API accepts JSON and inserts it verbatim: INSERT INTO users (email, name) VALUES (NULL, 'John') — the client omitted email. If the column is NOT NULL without a default, MySQL 8 rejects it with ERROR 1048 (23000): Column 'email' cannot be null. (On older MySQL 5.7 non-strict setups, the same insert produced a warning and stored an empty string — strict mode turned that silent behavior into a hard error.)
Run these to confirm the cause before applying a fix — each command answers a specific question.
SHOW COLUMNS FROM users;
The Null column shows NO for NOT NULL fields; Default shows whether the server can substitute a value instead of failing.
SELECT @@sql_mode;
STRICT_TRANS_TABLES turns silent NULL-to-empty coercion into ERROR 1048 — the same data 'worked' on non-strict 5.7.
The value is genuinely missing?
├─ required field → validate before INSERT (return 400 to the client, not a 500)
└─ optional field → give the column a DEFAULT
ALTER TABLE users ALTER email SET DEFAULT '';
The value arrives as the string 'null' or 'undefined'?
└─ serialization bug → fix the client (a JSON null is SQL NULL,
but the literal string 'null' is a value that fails validation instead)
Worked on MySQL 5.7, fails on 8.0?
└─ strict mode → keep it on and fix the data path (do not disable strict mode)
SHOW CREATE TABLE users; -- check the NULL / NOT NULL flags
Confirms whether the column is really NOT NULL and whether a DEFAULT exists.
Validate required fields before the query and let validation errors surface first.
if (!title || !authorId) {
return res.status(422).json({ error: 'title and authorId are required' })
}
Use nullable=False in the model so validation fails at the ORM boundary, not the DB.
class User(Base):
__tablename__ = 'users'
email = Column(String(255), nullable=False)
Check nulls before binding parameters.
if ($title === null) { throw new InvalidArgumentException('title required'); }
Most often this happens when inserting or updating a NOT NULL column with NULL (missing field in the ORM payload), or when a column added as NOT NULL without a default while existing rows are re-inserted.
Identify the column from the message and check why it is null in your data.
This page documents fixes for: nodejs-mysql2, python-sqlalchemy, php-pdo.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official MySQL documentation linked below and adds practical troubleshooting guidance on top.