TIL: Debugging "Column Cannot Be Null" Error in MySQL despite nullable column definition

Search for a command to run...

No comments yet. Be the first to comment.
Series on sharing simple insights I've learned. While some may seem basic, there are always others who, like me, may not be aware. Join me in discovering and sharing small, valuable lessons.
Suppose, we want to add an inline edit feature on a table row. It can be a little tricky to implement. I had a hard time implementing it because I wasn't about one of the concepts of HTML that we cannot add a form tag inside a table. Something like t...
Motivation Recently, I tried to learn some low-level system programming stuff. I am a Mac user, and I thought that everything that works on Linux should also work on Mac. After all, Mac is a Unix-based system 😊. I guess we all heard this. Oh boy! I ...

Recently, I was exploring design patterns courses on my LinkedIn Learning subscription. I came across a course, Node.js: Design Patterns by Alex Banks. It is a wonderful, easy-to-understand course. I started with the Builder Pattern, and the explanat...

Suppose you are working on a table in a LiveView project. This table has limited static data of not more than one page (you can avoid questions about pagination in the comment section 😊). From a user's point of view, it becomes hard to look into the...

Recently, while working on one of my personal Elixir projects. I came across a scenario where I needed to make some changes to the database schema.The changes mainly revolve around adding and removing indexes due to changes in the business requiremen...

While working on a Nestjs project, I encountered a weird problem related to the database column. I was trying to insert a record into a MySQL table using TypeORM. The error I was experiencing stated that a specific column “cannot be null”, even though the column was explicitly defined as nullable.
I performed the following checks to identify the root cause:
@Column({ type: 'int', nullable: true })
columnName?: number | null;
The column was correctly marked as nullable in the ORM model.
Verified Database Schema using SHOW CREATE TABLE
CREATE TABLE `table_name` (
`columnName` int DEFAULT NULL,
...
);
To make sure everything is good at the table structure level, I ran this command SHOW CREATE TABLE TABLE_NAME. This command gives the details of the table and how it is structured. The definition explicitly stated that the column could be null.
Checked for Foreign Key Constraints using SHOW CREATE TABLE & SHOW TABLE STATUS
No explicit foreign key constraint was found enforcing a non-null value.
Checked Active Triggers using SHOW TRIGGERS WHERE Table = 'table_name';
Discovered one or more triggers (e.g., table_name_insert, table_name_update) that were modifying data before the insert/update operation.
Issue Identified:
Option 1: Remove the Trigger (if unnecessary)
DROP TRIGGER trigger_name;
The easiest option to fix this problem was to remove the trigger, which is causing the problem.
Option 2: Modify the Trigger to Handle Null Values Properly
CREATE TRIGGER trigger_name
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
IF NEW.columnName IS NOT NULL THEN
INSERT INTO log_table (columnName, action)
VALUES (NEW.columnName, 'INSERT');
END IF;
END;
We can add a safety check to prevent errors when the inserted record is NULL.
Option 3: Allow NULL values in the log table
We can modify the column definition in the target log_table to allow NULLs.
Even if a column is nullable, triggers can override this behavior.
Always check active triggers using
SHOW TRIGGERS WHERE `Table` = 'table_name';
Modifying or disabling triggers can resolve unexpected Column Cannot Be Null errors.
I hope you like this small TIL blog. If you have any questions, please comment below. Thanks for reading 😊.