MySQL Error 1048: Column Cannot Be Null

MySQL error 1048 (Column Cannot Be Null) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.

MySQL Error Code 1048 MySQL Last verified 2026-08-19

Quick Answer

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.

Error Code

MySQL Error Code: 1048
Official name: Column Cannot Be Null
System name: Column cannot be null
Service: MySQL

What does this error mean?

Column '%s' cannot be null

Common Causes

How to Fix

  1. Identify the column from the message and check why it is null in your data
  2. Provide a value in the INSERT/UPDATE, or fix the application so the field is always set
  3. If the column should be optional, change it: `ALTER TABLE t MODIFY col VARCHAR(255) NULL;`
  4. If a default makes sense, add one: `ALTER TABLE t ALTER col SET DEFAULT 'x';`

Example Scenario

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.)

Diagnostic Commands

Run these to confirm the cause before applying a fix — each command answers a specific question.

Which columns reject NULL and which have defaults? sql
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.

Is strict mode making previously-tolerated inserts fail? sql
SELECT @@sql_mode;

STRICT_TRANS_TABLES turns silent NULL-to-empty coercion into ERROR 1048 — the same data 'worked' on non-strict 5.7.

Decision Guide

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)

Code Examples

Inspect the column definition sql
SHOW CREATE TABLE users;  -- check the NULL / NOT NULL flags

Confirms whether the column is really NOT NULL and whether a DEFAULT exists.

Framework-Specific Fixes

nodejs-mysql2

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' })
}

python-sqlalchemy

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)

php-pdo

Check nulls before binding parameters.

if ($title === null) { throw new InvalidArgumentException('title required'); }

You Might Also Like

Frequently Asked Questions

Why am I seeing MySQL error 1048?

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.

How do I fix MySQL error 1048?

Identify the column from the message and check why it is null in your data.

Which frameworks have documented fixes for error 1048?

This page documents fixes for: nodejs-mysql2, python-sqlalchemy, php-pdo.

Official Sources

This page is based on the official MySQL documentation linked below and adds practical troubleshooting guidance on top.