MySql错误:1364字段'display_name'没有默认值

时间:2022-11-14 23:55:34

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:

我刚刚从MAMP安装切换到本机Apache,MySql和PHP安装。我已经完成了所有工作,但我已经开始在新环境中使用我的Web应用程序,并且突然任何INSERT命令都会导致以下错误:

SQLSTATE[HY000]: General error: 1364 Field 'display_name' doesn't have a default value

SQLSTATE [HY000]:常规错误:1364字段'display_name'没有默认值

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

看来我现在无法将领域留空。我正在使用MySql版本5.6.13

Is there a way to change this setting in MySql?

有没有办法在MySql中更改此设置?

5 个解决方案

#1


110  

MySQL is most likely in STRICT mode.

MySQL很可能处于STRICT模式。

Try running SET GLOBAL sql_mode='' or edit your my.cnf to make sure you aren't setting STRICT_ALL_TABLES or the like.

尝试运行SET GLOBAL sql_mode =''或编辑my.cnf以确保您没有设置STRICT_ALL_TABLES等。

#2


13  

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.

MySQL最有可能处于严格模式,这不一定是坏事,因为你会尽早识别错误/问题,而不是盲目地认为一切都按预期工作。

Change the column to allow null:

将列更改为允许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 ''

#3


2  

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!

所有这些答案都是一个很好的方法,但我认为您不想总是转到您的数据库并修改您设置为NULL的默认值。我建议你去app / User.php并修改受保护的$ fillable,这样它就会把你新的字段用在用于注册的数据数组中。假设你添加一个名为“first_name”的新输入字段,你的$ fillable应该是这样的:protected $ fillable = ['first_name','email','password',];这样做对我来说。祝你好运!

#4


1  

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:

此外,我使用Laravel时遇到此问题,但通过更改我的数据库架构以允许在我计划从不同表单收集信息的表上允许“null”输入来修复:

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!

- > nullable();添加到最后。这是使用Laravel。希望这对某人有所帮助,谢谢!

#5


0  

I also faced that problem and there are two ways to solve this in laravel.

我也遇到了这个问题,有两种方法可以解决这个问题。

1.first one is you can set default value as null.I will show you an example.

1.第一个是你可以将默认值设置为null。我将给你一个例子。

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 above example you can set nullable() for that feature.then when you are inserting data mysql set default value as null.

如上例所示,您可以为该功能设置nullable()。然后在插入数据时,mysql将默认值设置为null。

  1. second one is in your model set your input field in protected $fillable field.as example

    第二个是在您的模型中设置您的输入字段在受保护的$ fillable field.as示例

    protected $fillable = [ 'name', 'email', 'password', 'slug', 'gender','pic' ];

    protected $ fillable = ['name','email','password','slug','gender','pic'];

I think second one is fine than first one and also you can set nullable feature as well as fillable in same time without a problem.

我认为第二个比第一个好,你也可以同时设置可空功能和可填写功能而没有问题。

#1


110  

MySQL is most likely in STRICT mode.

MySQL很可能处于STRICT模式。

Try running SET GLOBAL sql_mode='' or edit your my.cnf to make sure you aren't setting STRICT_ALL_TABLES or the like.

尝试运行SET GLOBAL sql_mode =''或编辑my.cnf以确保您没有设置STRICT_ALL_TABLES等。

#2


13  

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.

MySQL最有可能处于严格模式,这不一定是坏事,因为你会尽早识别错误/问题,而不是盲目地认为一切都按预期工作。

Change the column to allow null:

将列更改为允许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 ''

#3


2  

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!

所有这些答案都是一个很好的方法,但我认为您不想总是转到您的数据库并修改您设置为NULL的默认值。我建议你去app / User.php并修改受保护的$ fillable,这样它就会把你新的字段用在用于注册的数据数组中。假设你添加一个名为“first_name”的新输入字段,你的$ fillable应该是这样的:protected $ fillable = ['first_name','email','password',];这样做对我来说。祝你好运!

#4


1  

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:

此外,我使用Laravel时遇到此问题,但通过更改我的数据库架构以允许在我计划从不同表单收集信息的表上允许“null”输入来修复:

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!

- > nullable();添加到最后。这是使用Laravel。希望这对某人有所帮助,谢谢!

#5


0  

I also faced that problem and there are two ways to solve this in laravel.

我也遇到了这个问题,有两种方法可以解决这个问题。

1.first one is you can set default value as null.I will show you an example.

1.第一个是你可以将默认值设置为null。我将给你一个例子。

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 above example you can set nullable() for that feature.then when you are inserting data mysql set default value as null.

如上例所示,您可以为该功能设置nullable()。然后在插入数据时,mysql将默认值设置为null。

  1. second one is in your model set your input field in protected $fillable field.as example

    第二个是在您的模型中设置您的输入字段在受保护的$ fillable field.as示例

    protected $fillable = [ 'name', 'email', 'password', 'slug', 'gender','pic' ];

    protected $ fillable = ['name','email','password','slug','gender','pic'];

I think second one is fine than first one and also you can set nullable feature as well as fillable in same time without a problem.

我认为第二个比第一个好,你也可以同时设置可空功能和可填写功能而没有问题。