mysql - "column cannot be null" - mysql

as you see in the title, even if i removed "not null" feature from the related field, it still doesn't let me to insert null value for that field although the field is nullable!
Any help would be appreciated.
EDITED
Create:
CREATE TABLE `review` (
..
`RATING` int(11) DEFAULT NULL,
..
(`CATALOG_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=31625 DEFAULT CHARSET=latin5 ROW_FORMAT=DYNAMIC
Query:
INSERT INTO review (RATING,..) VALUES (null,..);
Error message:
Error: Column 'RATING' cannot be null
SQLState: 23000
ErrorCode: 1048
I also try to insert without RATING in the insert query, even if it is default null and nullable field, it gives the same error message and never inserts the field.

Bohemian, thanks for your attention. You are right, I figured out that there is a trigger for insert action which effects the related field. I disabled the trigger and the error is fixed. Thanks.

First of all look your "datetime / created_at / updated_at" field, do not assign the default value, select current time. after that you can can update the fields name

Related

SQL Error: 1364 - Field 'description' doesn't have a default value

I am getting this error when attempting to insert into mybb's mybb_settinggroups SQL table:
SQL Error: 1364 - Field 'description' doesn't have a default value
Query: INSERT INTO mybb_settinggroups (name,title,disporder,isdefault)
VALUES ('lock','Lock Settings',1,0)
How can I resolve this?
The field description would seem to be declared NOT NULL with no default value. So, you need to include it in the INSERT:
INSERT INTO mybb_settinggroups (name, title, disporder, isdefault, description)
VALUES ('lock', 'Lock Settings', 1, 0, 'no description available');
You may have a more reasonable description in mind.
You can either drop not null constraint or alter the table on description column
as below
alter table name alter column
description
set
default 'no description'

SQL Query not working on RDS

Using MySQL 5.6.40
This is the table definition:
CREATE TABLE IF NOT EXISTS `updated_tables` (
`table_name` VARCHAR(50) NOT NULL,
`updated_at` TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (`table_name`),
UNIQUE INDEX `table_name_UNIQUE` (`table_name` ASC))
ENGINE = InnoDB;
This is the query (Generated by Sequalize):
INSERT INTO `updated_tables` (`table_name`,`updated_at`) VALUES ('workdamnit',NULL) ON DUPLICATE KEY UPDATE `table_name`=VALUES(`table_name`), `updated_at`=VALUES(`updated_at`);
Simplified form of the same query:
INSERT INTO `updated_tables` (`table_name`,`updated_at`) VALUES ('workdamnit',NULL)
And it produces the following entry in table:
table_name: workdamnit
updated_at: 2018-07-05 14:27:17.142494
Now to the question.
Using MySQL 5.6.39-log on AWS RDS
Gives this error:
Error Code: 1048. Column 'updated_at' cannot be null
Is it since the MySQL versions are a bit different, or it has to do something with RDS?
During the creation of your table you have mentioned the following on your column: updated_at:
Do not allow NULL
If nothing is provided, use CURRENT_TIMESTAMP(6) as default.
Hence, It is giving the error when you are inserting NULL in your query.
So, If I understand what you are trying to do currectly, this should be your query:
INSERT INTO `updated_tables` (`table_name`) VALUES ('workdamnit')
instead of
INSERT INTO `updated_tables` (`table_name`,`updated_at`) VALUES ('workdamnit',NULL)
Hope it helps
You are trying to INSERT NULL on a field that does not allow it. Furthermore this field has a default value. so you can simply try:
INSERT INTO `updated_tables` (`table_name`) VALUES ('workdamnit') ...
After some research I found the answer. The RDS instance has a strict mode turned on by default, so it was responding properly. While my local instance, and also an instance of MySQL on EC2, were not using the strict mode.

MYSQL issue to change some date values in a column

I'm facing a problem with a shitty database in production done by someone before me.
I have a table called "projets" with some column with a date. Instead of having a column with NULL value accepted, it was created with no NULL value authorized. So instead of a NULL, all entry are 0000-00-00.
So i've change a bit the column to insert NULL value inside like this
ALTER TABLE `projets`
ALTER `date_avant_projet` DROP DEFAULT;
ALTER TABLE `projets`
CHANGE COLUMN `date_avant_projet` `date_avant_projet` DATE NULL AFTER `procede`;
So now the column can normally accept NULL value. So i try to do this to change all 0000-00-00 to NULL
update projets set date_avant_projet = NULL where date_avant_projet = '0000-00-00'
But like that i've got an SQL error 1292 : Incorrect date value: '0000-00-00' for column 'date_avant_projet' at row 1.
It's very annoying for me because i have to also create a new column with a foreign key but when i try this
ALTER TABLE `projets`
ADD COLUMN `id_dernier_changement_etat` INT NOT NULL AFTER `etat`,
ADD CONSTRAINT `FK_projets_id_changement_etat` FOREIGN KEY (`id_dernier_changement_etat`) REFERENCES `changement_etat_projet` (`id`);
I've got EXACTLY the same error message : SQL error 1292 : Incorrect date value: '0000-00-00' for column 'date_avant_projet' at row 1
If someone understand better than me, it will be very helpful!
Thanks in advance for your future answer.

Mysql update: ERROR 1292 (22007): Truncated incorrect DOUBLE value

My problem is the following:
I have a table foo_table with id column ID as bigint (primary key), name column NAME as varchar(80) and code COLUMN code as varchar(20). I have a unique constraint on the CODE column.
If I fill this table with only "number" strings in the code column, and then one day decide to add a row with a, surprise, "string" string in that code column, say putting "My Code", it works fine ! The error comes when I tried updating one of my previous records, one that has a CODE value being a number; in this case I get the famous error saying:
ERROR 1292 (22007): Truncated incorrect DOUBLE value: 'My Code'.
I'm not even touching that record ! I presume MySQL is trying to check the unique constraint and try to convert every value in the CODE column to double, but that's insane, and very unprofessional.
Anyone has a solution ? Like turning off string<->number conversion in MySQL ?
Thanks in advance.
Edit:
Ok I have the sample code, but while creating it I saw how to fix the problem, which is still a problem in MySQL as far as I'm concerned:
CREATE TABLE foo_table (
ID bigint(20) NOT NULL DEFAULT '0',
NAME varchar(80) DEFAULT NULL,
CODE varchar(20) NOT NULL DEFAULT '',
UNIQUE KEY foo_table_uk (CODE)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
insert into foo_table (id, name, code) values (1, 'one', '1');
insert into foo_table (id, name, code) values (2, 'two', '2');
insert into foo_table (id, name, code) values (3, 'three', 'three');
update foo_table set name='a-one', code='1' where code=1; <-- This update throws the error
update foo_table set name='a-one', code='1' where code='1'; <-- this update works fine

Mandatory field mysql

How to make sure that field is mandatory ? Here is what I mean
I have the following mysql table structure:
CREATE TABLE `new` (
`id` int(11) DEFAULT NULL,
`name` int(11) DEFAULT NULL,
`phone` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Here is query with no data for phone
insert into new values(1, 'm', '');
But the query runs fine. What should be done so that mysql returns an error if there is no data for phone field? I can do that validation by php, but I'm curious how to do that in mysql.
Possibly setting the default value of the 'phone' column to NULL would make it fail insertion because it would end up null if you did not specify it.
Otherwise you're going to need to omit the phone column for the default to kick in, say in php you'd use empty($phone) ? null : $phone; or something along those lines.
INSERT INTO new VALUES(1,'m',NULL)
will cause error.
If you want to check whether is the phone number field is a blank string,
you can use a trigger in MySQL.
I haven't tested this, but I have a feeling the '' != null. What happens if you run
insert into new(id, name) values (1, 'test');
I bet you get an insert error...
Anyway, I think its probably better to be validating in PHP than waiting till you get to the database... inserts are expensive...
'' as the 3rd option doesnt make the value of phone null.. It is just equal to a blank string thats all.
if you want to see an error, replace '' with NULL.