I have a table with a column that is filled by a before insert trigger, this column is set to be NOT NULL and has no DEFAULT VALUE.
When I do a INSERT TABLE without passing this column, I receive the error: 1364 - Field 'column_name' doesn't have a default value. I'd search the web for a solution and have encountered this question: Field 'id' doesn't have a default value?. I then checked and changed the mysql_mode from:
"STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
to:
"NO_ENGINE_SUBSTITUTION"
but the insert query still returns the same error.
What should I do to bypass this "strict" mode? Or is it something else?
Please let me know if I need to include any config variable. I'm using mysql 5.6.39.
This is a known bug for the mysql 5.6.39 version. And has been fixed since version 5.7.
Related
I'm developing a feature to show to the users how many unread messages there is on the site. So I'm trying to execute this query:
alter table messages add read boolean not null default 0
But the server returns this message:
#1067 - Invalid default value for 'Data'
I can't see what is wrong. I'm running MariaDB/MySQL 5.7.20-0ubuntu0.16.04.1.
Found the problem. The "Data" column is with a invalid default value. I got this database restored from a server that has a different MySQL version.
I keep getting this error for an insert into ... update query, the problem, this column 'str' does not exist in the table being updated, or any of the tables I'm pulling data from, and it's not in the query.
Error Code: 1406. Data too long for column 'str' at row 215710
I'm totally stumped here. It this a mysql bug? I went as far as to isolate the query to just one column, still got this error.
UPDATE 1:
I just tried updating with a manual value, on one column only set to longtext. I'm still getting the exact same error.
UPDATE 2:
Major update, I isolated the problem down to the select query, the original error implied a table column, however, it seems to be pointing to what I assume is some kind of temp table column for the following row. When I yanked this out of the query, it worked. Ironically, this is the same column I did my one column test with where I manually entered an value in the update on duplicate key part of my query.
CONCAT_WS('', UC_Words(`name`), ' | ', UC_Words(`city`), ' ', UC_Words(`state`), ' ', UC_Words(`country`), CONCAT('|---|',`name-key`)) AS `owner-data`
I'm currently using lots of GROUP_CONCAT's, but I have already adjusted the length. Is there a parameter for CONCAT_WS length? NOTE: UC_Words is a custom function. This could possibly be a culprit, still need to test it...
UPDATE 3:
The error appears to be a result of the UC_Words function. The 'str' is the name field in that function. Type was set to VARCHAR 255, which was too short.
MySQL will truncate any insert value that exceeds the specified column width.
to make this without error try Switch your MySQL mode to not use STRICT.
EDIT:
To change the mode
This can be done in two ways:
Open your "my.ini" file within the MySQL installation directory, and look for the text "sql-mode".
Find:
Code:
Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Replace with:
Code:
Set the SQL mode to strict
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Or
You can run an SQL query within your database management tool, such as phpMyAdmin:
Code:
SET ##global.sql_mode= '';
The error appears to be a result of the UC_Words function. The 'str' is the name field in that function. Type was set to VARCHAR 255, which was too short.
I am aware that the certain field doesn't have a default value.
I've been using MySQL 5.5.28 and it does work whenever I insert without specifying a value on that field. The field is TINYINT and by default, without specifying any value AND without declaring a default value during creation of the table, a value of 0 will be inserted in that field during INSERT statement.
However, after updating to MySQL 5.5.30, the query doesn't work anymore and is returning Field doesn't have a default value.
I've been looking through the changelogs and didn't find any clue that something has changed with regards to the default values of Integer.
MySQL 5.5.29 : http://dev.mysql.com/doc/relnotes/mysql/5.5/en/news-5-5-29.html#mysqld-5-5-29-feature
MySQL 5.5.30 : http://dev.mysql.com/doc/relnotes/mysql/5.5/en/news-5-5-30.html
Test queries:
MyTable has the Fields MyField1 and MyField2
INSERT INTO MyTable(MyField2)VALUES('MICHAEL');
Result on MySQL 5.5.28:
MyField1 | MyField2
0 | MICHAEL
With warning: 1 row(s) affected, 1 warning(s): 1364 Field 'MyField1' doesn't have a default value
Result on MySQL 5.5.30:
No changes on data and throws an error
Error Code: 1364. Field 'MyField1' doesn't have a default value
INSERT INTO MyTable(MyField1, MyField2)VALUES(0, 'MICHAEL');
The above query will work though.
In the 1st server strict sql mode was not enabled, while in the 2nd one it was. Read more about strict mode in the mysql documentation.
Specifically:
If strict mode is not in effect, MySQL inserts adjusted values for invalid or missing values and produces warnings (see Section 13.7.5.40, “SHOW WARNINGS Syntax”). In strict mode, you can produce this behavior by using INSERT IGNORE or UPDATE IGNORE.
I have a MySQL table with some dates,
I need that one of them have a default value equal to current time,
I'm using 'Sequel Pro' to build the database,
Then i wrote 'now()' (and 'GETDATE()') in default value, but doesn't work.
Can someone do help me, How 'Set Default value' to 'NOW()' with Sequel PRO?
ERROR:
An error occurred when trying to change the field 'DataDoPedido' via
ALTER TABLE Reserva CHANGE DataDoPedido DataDoPedido DATE
NOT NULL
DEFAULT 'now()'
MySQL said: Invalid default value for 'DataDoPedido'
thanks.
For MySQL, the DEFAULT specified for a column must be a constant; it cannot be the return from a function. The one exception to this is the TIMESTAMP datatype, which can have a DEFAULT CURRENT_TIMESTAMP.
If you need to initialize a DATE column, one workaround is to create a BEFORE INSERT ON trigger.
So I receive "Column 'column_name' cannot be null" whenever I try to insert NULL into a column that is specified with NO NULL.
I can do INSERT IGNORE which would work fine but I prefer not to do it that way.
I am pretty sure there is a global variable that I can set so to disable this validation in db.
I thought I enabled strict-mode in ##global.sql_mode but it was empty.
Please let me know if you know which variable I should change to disable this error!
Many thanks guys!
ALTER TABLE table_name ALTER column_name SET DEFAULT default_value;
I suppose that the not null flag was with a point. Add the default_value to your liking ( I guess 0 ). Also this works if the column is NOT text/blob.