2

Why does mysql accepts null data when updating a not null column and then converts the data to 0.

I am expecting an error it just does not show up. How can I get an error if someone tries to update a not null column to null? I need it so I can rollback the transaction if I get an error.Is there any configuration needed within the database to do this? Thank you

enter image description here

6
  • are there default value ?can you show the ddl of the table , and the update you are running Commented Oct 6, 2017 at 7:22
  • Show us your migration file at least which uses this column and show us database.php config array (without db credentials of course) Commented Oct 6, 2017 at 7:23
  • One more thing, are you using your own handlers or rewrited the vanilla handler? Commented Oct 6, 2017 at 7:24
  • @Moudiz im trying to update like $payment->payment_id = null; $payment->save() and it doesnt show error. the update was successful and updates payment_id value to 0 Commented Oct 6, 2017 at 7:33
  • @aaron0207 I just added my database structure in my question. Commented Oct 6, 2017 at 7:40

3 Answers 3

2

You've not specified which version of Mysql you're using, and in which mode. I'll answer this assuming you're running Mysql 5.7 without strict mode.

Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.) Strict mode also affects DDL statements such as CREATE TABLE.

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.

Source: https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-full

I recommend you to enable strict mode (STRICT_ALL_TABLES) and fix your application to support it; this will also enforce other query limitations where people are most commonly hit by ONLY_FULL_GROUP_BY.

To set the SQL mode at server startup, use the --sql-mode="modes" option on the command line, or sql-mode="modes" in an option file such as my.cnf (Unix operating systems) or my.ini (Windows). modes is a list of different modes separated by commas. To clear the SQL mode explicitly, set it to an empty string using --sql-mode="" on the command line, or sql-mode="" in an option file.

To change the SQL mode at runtime, set the global or session sql_mode system variable using a SET statement:

SET GLOBAL sql_mode = 'modes';
SET SESSION sql_mode = 'modes';

Source: https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-setting

Sign up to request clarification or add additional context in comments.

4 Comments

How can I enable strict mode?
I've updated the answer with a quote from the docs about setting the sql mode.
Thank you. I think this is the right answer. Im gonna with STRICT_TRANS_TABLES since I only need in in my transaction so I can rollback.
What about recent versions of mysql? Is it still possible to insert NULL into NOT NULL column?
0

Why does mysql accepts null data when updating a not null column and then converts the data to 0.

You question is not clear as we need the DDL of the table and the update , but as from what you are saying, Well logically because the column not null has a default value 0. check the below example.

create table Test_table ( name varchar(100) null , position_s varchar(100) default 'Y' not null)
SQL>     
Table created
insert into Emp_table (name) values('Me')
SQL>    
1 row inserted
SQL> 




NAME                                                                             POSITION_S
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
Me                                                                               Y

3 Comments

the default value is 'None'
no 'none' as in no default value. please check my db structure. I added it in my question
yes i remove it my comment after i noticed your edit, it seems something in php how its getting the 0 value ?, can you show us the php code ?
0

@aaron0207 @Moudiz I am using laravel and updates data like this.

$specificReservation = Reservation::where('reference_id',$reference_id)->first();
 $specificReservation->res_status = 1;
 $specificReservation->payment_id = null;
 $specificReservation->save();

I also tried to update manually in the database with this

 UPDATE reservations SET payment_id = null

and it also shows no error so I think this is a database problem.

1 Comment

please add this in your question and check sisve answet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.