Error
Error Code:
1048
MariaDB Error 1048: Null Value Not Allowed
Description
This error occurs when a SQL `INSERT` or `UPDATE` statement attempts to store a `NULL` value into a database column that is defined with a `NOT NULL` constraint. It signifies that a required data field has been left empty or explicitly set to `NULL`, violating the table's schema definition.
Error Message
Column '%s' cannot be null
Known Causes
3 known causesApplication Did Not Provide Data
An application or script attempted to insert or update a row without providing a value for a column that is marked as `NOT NULL`.
SQL Query Omitted Value
The `INSERT` or `UPDATE` SQL statement explicitly omitted a column that is defined as `NOT NULL`, or explicitly set its value to `NULL`.
No Default Value Set
The `NOT NULL` column does not have a `DEFAULT` value defined, meaning the database cannot automatically provide a value if one is not supplied.
Solutions
3 solutions available1. Provide Value for NOT NULL Column easy
Include value in INSERT statement
1
Include all NOT NULL columns in INSERT
-- Wrong: missing required column
INSERT INTO users (email) VALUES ('test@example.com');
-- Correct: include name (NOT NULL column)
INSERT INTO users (name, email) VALUES ('John', 'test@example.com');
2. Set Default Value for Column easy
Add default so NULL isn't needed
1
Alter column to have default value
ALTER TABLE users ALTER COLUMN status SET DEFAULT 'active';
2
Or modify column definition
ALTER TABLE users MODIFY COLUMN status VARCHAR(20) NOT NULL DEFAULT 'active';
3. Allow NULL Values easy
Remove NOT NULL constraint if appropriate
1
Make column nullable
ALTER TABLE users MODIFY COLUMN phone VARCHAR(20) NULL;