MySQL ERROR 1048: Column cannot be null means a statement tried to put NULL into a column declared NOT NULL, and MySQL refused. The message names the column, so the question is not which one, but where the NULL came from and why a value you thought was there turned into NULL.
mysql> INSERT INTO customers (email, status) VALUES (NULL, 'new');
ERROR 1048 (23000): Column 'email' cannot be null
mysql> UPDATE orders SET qty = NULL WHERE id = 1;
ERROR 1048 (23000): Column 'qty' cannot be null
The server error reference lists it as ER_BAD_NULL_ERROR, SQLSTATE 23000, the integrity constraint violation class that duplicate keys and foreign key errors also belong to. Many drivers map that whole class to one exception type, so check the error code, not only the exception class, when you log it.
A DEFAULT does not replace an explicit NULL
This is the surprise behind most 1048 errors in application code. A column defined as status VARCHAR(10) NOT NULL DEFAULT 'new' gets its default only when the column is left out of the statement, or when you write DEFAULT as the value. If the statement passes NULL explicitly, MySQL treats it as a request to store NULL. That fails.
ORMs and query builders cause this all the time. A model field that is unset in code is often sent as NULL instead of being left out of the INSERT. The fix is in the application: do not send the column at all, send DEFAULT, or map NULL to the default value in the statement.
-- status has DEFAULT 'new', but an explicit NULL is still NULL
INSERT INTO customers (email, status) VALUES ('a@example.com', NULL); -- 1048
-- leave the column out, or ask for the default explicitly
INSERT INTO customers (email) VALUES ('a@example.com');
INSERT INTO customers (email, status) VALUES ('a@example.com', DEFAULT);
-- or map NULL to the default in the statement itself
INSERT INTO customers (email, status)
VALUES ('a@example.com', COALESCE(?, 'new'));
If you leave out a NOT NULL column that has no default, you get a different error: 1364 Field '%s' doesn't have a default value. That one says "you gave me nothing", while 1048 says "you gave me NULL".
Error, warning or silent zero: it depends on the statement
The same NULL is handled in different ways, and this explains most "it worked on the other server" stories. According to the INSERT documentation and the data type defaults page:
- Single-row INSERT: always an error, with or without strict mode.
- Multi-row INSERT and INSERT ... SELECT, strict mode on (the default): an error. On InnoDB, the statement is rolled back.
- The same statements without strict mode: a warning, and the column gets the implicit default for its type:
0for numbers, an empty string for strings, the "zero" value for dates and times.INSERT ... SELECTcounts as multi-row even when it returns one row, because the server does not check the result set. - UPDATE: an error in strict mode. Without strict mode, a warning and the implicit default.
- INSERT IGNORE / UPDATE IGNORE: a warning and the implicit default, even in strict mode.
- LOAD DATA: a
\N(NULL) in the file for aNOT NULLcolumn reports a related error, 1263 Column set to default value; NULL supplied to NOT NULL column. WithIGNOREorLOCALit becomes a warning and the implicit default is stored (LOAD DATA documentation).
The non-strict paths are the dangerous ones. A missing email silently becomes '' and a missing quantity becomes 0, and nothing fails. Those rows then pass NOT NULL checks forever. If you inherit a database that ran without strict mode for years, look for those placeholder values. The same applies to the other truncation problems described in our guide to MySQL error 1265.
-- which columns are NOT NULL, and which have a default?
SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_DEFAULT, EXTRA
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'app' AND TABLE_NAME = 'customers'
ORDER BY ORDINAL_POSITION;
-- strict or not, in this session?
SELECT @@SESSION.sql_mode;
How to fix it
- Find the source of the NULL. Log the actual statement and parameters, not the ORM call. Typical sources: an optional form field, a failed lookup that returned nothing, a
LEFT JOINin anINSERT ... SELECTthat producedNULLfor unmatched rows, or a new column the application does not know about yet. - Decide whether NOT NULL is right. If the value really can be unknown, the column should allow
NULL. Putting''or0in place of "unknown" is how bad data starts. - If it must be NOT NULL, give it a value. Leave the column out so the default applies, use
DEFAULT, or useCOALESCE()inINSERT ... SELECT. In our experience, aBEFORE INSERTtrigger that replacesNULLalso works, but it hides the problem from anyone reading the application code, so use it only as a stopgap. - Do not switch strict mode off to get past it. Single-row inserts would still fail, and every multi-row insert and update would start writing zeros and empty strings without an error.
Errors with TIMESTAMP columns after an upgrade? When the deprecated explicit_defaults_for_timestamp is disabled, assigning NULL to a TIMESTAMP NOT NULL column stores the current timestamp instead of failing. In MySQL 8.4 the default is ON, which turns that off (system variables reference). Code written years ago that inserts NULL into created_at on purpose starts getting 1048 when it moves to a server with the default setting. Check this variable whenever you move between servers or configs. For the rest of the upgrade checklist, see our MySQL 8.0 to 8.4 upgrade guide.
Adding NOT NULL to a large table
On a small table, ALTER TABLE ... MODIFY ... NOT NULL is a one-liner. On a table with hundreds of millions of rows and constant writes, it is a project. These are the points that matter:
- The ALTER fails if a single NULL is left. According to the online DDL documentation, making a column
NOT NULLrebuilds the table in place, requiresSTRICT_ALL_TABLESorSTRICT_TRANS_TABLES, and fails if the column containsNULLvalues. In strict mode, the error you get is 1138 Invalid use of NULL value. On a large table the rebuild can take hours, so count the remainingNULLrows right before you start. - Without strict mode, the ALTER takes the copy path and rewrites your data. The same
MODIFYin a non-strict session cannot run in place (an explicitALGORITHM=INPLACEis refused with error 1846), so it copies the table, converts everyNULLto the implicit default and only reports 1265 truncation warnings. Run schema changes in a session with the SQL mode you expect, and check it in your migration tool too. - Backfill first, in batches. One
UPDATEover the whole table holds locks on every row it changes until it commits, and creates a huge transaction for replicas to apply. Small batches keep both under control. In our experience, a few thousand rows per batch with a short pause between batches is a safe starting point. Adjust it while you watch replica lag. - Close the gap before the ALTER. Deploy the application change that always writes a value before you backfill. Otherwise new
NULLrows keep arriving while the backfill runs, and the ALTER fails at the end anyway. - Multi-row batches fail as a unit. In strict mode, one
NULLin a 10,000-rowINSERTrolls the whole statement back on InnoDB. For bulk loads with messy input, load into a staging table that allowsNULL, fix or reject the bad rows there, then move the clean data.
-- 1. how many rows would block the change?
SELECT COUNT(*) FROM orders WHERE delivery_note IS NULL;
-- 2. backfill in small batches (repeat until 0 rows affected)
UPDATE orders SET delivery_note = ''
WHERE delivery_note IS NULL
LIMIT 5000;
-- 3. only then add the constraint (an in-place table rebuild)
ALTER TABLE orders
MODIFY delivery_note VARCHAR(255) NOT NULL DEFAULT '',
ALGORITHM=INPLACE, LOCK=NONE;
Key takeaways
- ERROR 1048 (23000) means an explicit
NULLreached aNOT NULLcolumn. ADEFAULTonly applies when the column is left out or set toDEFAULT. - 1364 means no value and no default, 1263 is the
LOAD DATAvariant, and 1138 is the ALTER that findsNULLrows. - Single-row inserts always fail. Multi-row statements and updates fail in strict mode, but without it they silently store
0or''. - Check
explicit_defaults_for_timestampwhenTIMESTAMPinserts start failing after a move or upgrade. - On large tables, fix the application first, backfill in batches, count the remaining
NULLrows, and only then run theNOT NULLrebuild, in strict mode.