-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
NULL value is not accepted for changes to a column where a DEFAULT value defined #1568
Comments
Please provide a pull request to patch the issue, thank you! |
@dougwilson Ok, I will do my best to submit a pull request when I get some time a bit later to isolate the issue and propose a solution. Thanks |
Awesome! If you're not able to, we can always look into this issue, but we need a lot more information in order to replicate the issue, like the version of Node.js, the version of this module, the version of your MySQL server, any system variables that change the strictness in your SQL server, all necessary DDL to setup the tables to replicate, and finally the JavaScript code to run in order to reproduce the issue. Let me know if you want to go down this path instead of submitting a PR :) |
@garudacrafts
I was curious about this, and just tried this out with MySQL 5.7 Command Line Client on Windows with: CREATE TABLE `new_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`test_default_nn` varchar(45) NOT NULL DEFAULT 'This is default value - NN',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; Using the Command Line Client I cannot insert
I can only get default if I do not insert any value into that field, e.g.:
EDIT: I can also get default by inserting
So this is to say that I don't think that your assertion is correct that:
My experience is that MySQL only applies the default value to a column if no value is specified or the keyword |
@bbito Indeed, I replicated your results! I thought I was taking crazy pills for a sec, then I realized that I was able to successfully run my INSERT statements with NULLs when multiple rows are added in a single statement execution, like so:
But, this resulted in empty values for the 'test_default_nn' column, NOT the DEFAULT, as I was expecting. I was mistaken. I had thought the inserts were applying the default! So, it seems this issue is better off as a feature request as previously filed in #559 , and not a bug. I will close it. Thanks for your quick response! |
Yeah, I came across an article about this MySQL "Quirk": |
@garudacrafts - Interestingly, I can't run your SQL on my MySQL 5.7.11 - it still errors mysql> INSERT INTO new_table VALUES (NULL, NULL), (NULL, NULL), (NULL, NULL);
ERROR 1048 (23000): Column 'test_default_nn' cannot be null Maybe this "quirk" was "fixed"? |
For additional reference (and some back story), in my script I have something like:
Now, |
@garudacrafts I'm curious what happens if you |
@bbito - I was able to successfully run that command, probably because my MySQL mode is not set to strict. You could try to turn off strict mode with |
@bbito - Yes, I confirmed a simple work-around is to check if |
So looks like a bunch of conversation occurred since I last dropped by, and the issue was even closed. I assume everything is good to go, then? The discussion sounds like it's said that this is not actually a bug in this module, rather a misunderstanding of how MySQL works, so I tagged it as a MySQL Server question. |
@dougwilson I think the only open question out of this thread relating to this module is: |
@dougwilson Got it - Thanks for the reply! |
@dougwilson - Yes, the issue as filed is not a bug, but rather stems from my misunderstanding of how MySQL handles NULL for columns with DEFAULT values set. Although it does illuminate the need for a mechanism to pass MySQL DEFAULT. I've submitted pull request mysqljs/sqlstring#11. |
@dougwilson & @bbito - I just re-read the MySQL doc on default value data types (http://dev.mysql.com/doc/refman/5.7/en/data-type-defaults.html) and found something interesting that relates to this discussion. If an UPDATE statement sets the column value to NULL, then MySQL will apply the implicit DEFAULT value for the column data type in certain cases (namely, when the column is defined as NOT NULL and has no explicit DEFAULT value defined, and strict mode is not enabled). I think I've used this on auto_increment INT's (e.g. primary keys) and timestamps before, which what led to my confusion that the same applied on INSERT statements (not true!). |
hello i create a table in mysql and i put one column NOT NULL that is how i create my table:CREATE TABLE
|
@lebog try setting sql_mode to STRICT_ALL_TABLES. Further further help, try Stack Overflow. |
If a null value is passed as a column value in an INSERT or UPDATE statement when a function call to query is made and that column is defined to have a DEFAULT value, then that default value should be applied to the column on the the row insert/update operation. This is the standard behavior for MySQL.
But, the mysql module returns the response "ER_BAD_NULL_ERROR: Column '[column_name]' cannot be null". The NPM doc for mysql states that "undefined / null are converted to NULL" under the Escaping Query Values section (https://www.npmjs.com/package/mysql#escaping-query-values). If JavaScript null is passed and converted to MySQL NULL, then the mysql module should process it like a regular SQL query that has NULL value passed, in which the column's DEFAULT value is applied.
This issue is related to the feature request #559. However, I am not suggesting js undefined should act like MySQL NULL in regards to applying the column default value. I'm saying there is a bug on SQL statement execution: the mysql module does not properly behave when MySQL NULL is passed after (correctly being converted from) a js null value.
The text was updated successfully, but these errors were encountered: