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

Open
xOldeVx opened this issue Dec 19, 2024 · 0 comments
Open

mysql2 throw error instead of fit the default value #3288

xOldeVx opened this issue Dec 19, 2024 · 0 comments

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,
    ]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant