Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL #1364 - Field 'column_name' doesn't have a default value - Can't insert into DB [duplicate]

I recently moved my MySQL database to a new server and it has given me some problems i have newer experienced with MySQL before. My table columns are set to "Default => None" and my table has been generating the default value depending on the Datatype. But now when i try to insert into a table i'm getting this error message: "#1364 - Field 'column_name' doesn't have a default value" and then nothing is inserted into the table.

What can i do to make the "Default" choose it's own value?

like image 271
NIKO_B Avatar asked Sep 24 '16 08:09

NIKO_B


1 Answers

It's not saving into the database definitely because the field 'column_name' (and maybe some others) is checked as "NOT NULL". It means that the value of that field must be something other than NULL (NULL - no data at all)

Marking fields as not null is usually a great way to ensure that some data will always be present in the field. Depending on your needs, you can also mark it as NULL so it will never throw an error and will save into DB without the need for anything to be inserted into a specified field.

It means you have 2 options:

  1. Mark your field as NULL (first check if your field is required to have some value or not).

    ALTER TABLE `your_table` 
    CHANGE COLUMN `your_field` `your_field` VARCHAR(250) NULL;
    
  2. Add a default value to the field so if no data is provided on insert, it will put something you defined. For example:

    ALTER TABLE `your_table` CHANGE COLUMN `your_field` `your_field` VARCHAR(250) NOT NULL DEFAULT 'some_default_value';
    

And of course, match your field type to the field you are going to change.

like image 73
AwesomeGuy Avatar answered Sep 21 '22 13:09

AwesomeGuy