MySql Error: 1364 Field 'display_name' doesn't have default value
PhpMysqlPhp Problem Overview
I have just switched from a MAMP installation to a native Apache, MySql and PHP installation. I have got everything working, but I have started using my web app in the new environment and suddenly any INSERT commands are resulting in the following error:
> SQLSTATE[HY000]: General error: 1364 Field 'display_name' doesn't have a default value
It seems the I am unable to leave a field blank now where I was able to before. I am using MySql version 5.6.13
Is there a way to change this setting in MySql?
Php Solutions
Solution 1 - Php
MySQL is most likely in STRICT mode.
Try running SET GLOBAL sql_mode=''
or edit your my.cnf to make sure you aren't setting STRICT_ALL_TABLES
or the like.
Solution 2 - Php
MySQL is most likely in STRICT
mode, which isn't necessarily a bad thing, as you'll identify bugs/issues early and not just blindly think everything is working as you intended.
Change the column to allow null:
ALTER TABLE `x` CHANGE `display_name` `display_name` TEXT NULL
or, give it a default value as empty string:
ALTER TABLE `x` CHANGE `display_name` `display_name` TEXT NOT NULL DEFAULT ''
Solution 3 - Php
All of these answers are a good way, but I don't think you want to always go to your DB and modify the default value that you have set to NULL.
I suggest you go to the app/User.php and modify the protected $fillable
so it will take your new fields in the data array used to register.
Let's say you add a new input field called "first_name"
, your $fillable
should be something like this :
protected $fillable = [ 'first_name', 'email', 'password',];
This did it for me. Good luck!
Solution 4 - Php
I also faced that problem and there are two ways to solve this in laravel.
-
first one is you can set the default value as null. I will show you an example:
public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('gender'); $table->string('slug'); $table->string('pic')->nullable(); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
as the above example, you can set nullable()
for that feature. then when you are inserting data MySQL set the default value as null.
-
second one is in your model set your input field in
protected $fillable
field. as example:protected $fillable = [ 'name', 'email', 'password', 'slug', 'gender','pic' ];
I think the second one is fine than the first one and also you can set nullable feature as well as fillable in the same time without a problem.
Solution 5 - Php
Also, I had this issue using Laravel, but fixed by changing my database schema to allow "null" inputs on a table where I plan to collect the information from separate forms:
public function up()
{
Schema::create('trip_table', function (Blueprint $table) {
$table->increments('trip_id')->unsigned();
$table->time('est_start');
$table->time('est_end');
$table->time('act_start')->nullable();
$table->time('act_end')->nullable();
$table->date('Trip_Date');
$table->integer('Starting_Miles')->nullable();
$table->integer('Ending_Miles')->nullable();
$table->string('Bus_id')->nullable();
$table->string('Event');
$table->string('Desc')->nullable();
$table->string('Destination');
$table->string('Departure_location');
$table->text('Drivers_Comment')->nullable();
$table->string('Requester')->nullable();
$table->integer('driver_id')->nullable();
$table->timestamps();
});
}
The ->nullable(); Added to the end. This is using Laravel. Hope this helps someone, thanks!
Solution 6 - Php
I got this error when I forgot to add new form fields/database columns to the $fillable
array in the Laravel model - the model was stripping them out.
Solution 7 - Php
I also had this issue using Lumen, but fixed by setting DB_STRICT_MODE=false
in .env
file.