php MySql 错误:1364 字段“display_name”没有默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18751291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:09:16  来源:igfitidea点击:

MySql Error: 1364 Field 'display_name' doesn't have default value

phpmysql

提问by Ben Thompson

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 安装。我已经让一切正常,但我已经开始在新环境中使用我的网络应用程序,突然任何 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 中更改此设置?

回答by Tim B

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_TABLESor the like.

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

回答by calcinai

MySQL is most likely in STRICTmode, 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 最有可能处于STRICT模式中,这不一定是坏事,因为您会及早发现错误/问题,而不是盲目地认为一切都按您的预期工作。

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 ''

回答by Stefan Paius

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.

所有这些答案都是一个好方法,但我认为您不想总是去您的数据库并修改您设置为NULL的默认值。

I suggest you go to the app/User.phpand modify the protected $fillableso it will take your new fields in the data array used to register.

我建议你去app/User.php并修改protected $fillable它,这样它就会在用于注册的数据数组中使用你的新字段。

Let's say you add a new input field called "first_name", your $fillableshould be something like this :

假设您添加了一个名为 的新输入字段"first_name",您$fillable应该是这样的:

protected $fillable = [ 'first_name', 'email', 'password',]; 

This did it for me. Good luck!

这为我做到了。祝你好运!

回答by Nimeshika Prabodhani

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

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

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

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

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

    protected $fillable = [
        'name', 'email', 'password', 'slug', 'gender','pic'
    ];
    
  1. 第二个是在您的模型中设置您的输入protected $fillable字段。例如:

    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.

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

回答by Andrew Koper

I got this error when I forgot to add new form fields/database columnsto the $fillablearray in the Laravel model - the model was stripping them out.

当我忘记将新的表单字段/数据库列添加$fillableLaravel 模型中的数组时,我收到此错误- 模型正在将它们剥离。

回答by Ryan Jarvis

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 时遇到了这个问题,但通过更改我的数据库架构以允许在我计划从单独表单收集信息的表上输入“空”来解决此问题:

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。希望这对某人有所帮助,谢谢!