Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql2 throw error instead of fit the default value #3288

Closed
xOldeVx opened this issue Dec 19, 2024 · 1 comment
Closed

mysql2 throw error instead of fit the default value #3288

xOldeVx opened this issue Dec 19, 2024 · 1 comment

Comments

@xOldeVx
Copy link

xOldeVx commented Dec 19, 2024

I have a column with default value and not nullable, when I try to insert null value to this column i'm getting error Column 'isUsa' cannot be null, why mysql2 doesn't fit the default value instead of throw exception?

For example in sql query:

-- Insert without specifying isUsa, will default to 0
INSERT INTO users () VALUES ();

-- Insert with NULL value for isUsa, will be replaced with 0
INSERT INTO users (isUsa) VALUES (NULL);

This is the user table describe:

Field	Type	Null	Key	Default	Extra
isUsa	tinyint(1)	        NO		  0	
.....
.....

This is the backend snippet:

const addUserDB = async (user) => {
    const userQuery = 'INSERT INTO users (firstName, lastName, isUsa) VALUES (?, ?, ?)';

    await db.query(userQuery, [
        user.firstName,
        user.lastName,
        user.isUsa,
    ]);
}
@sidorares
Copy link
Owner

@xOldeVx I see exactly the same error when I do the query from the command line:

mysql> INSERT INTO users (isUsa) VALUES (NULL);
ERROR 1048 (23000): Column 'isUsa' cannot be null

I don't see any issue with the driver here. Feel free to reopen if you have more information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants