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

后端 未结 7 1766
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 21:37

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 e

相关标签:
7条回答
  • 2020-11-28 21:41

    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!

    0 讨论(0)
  • 2020-11-28 21:46

    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.

    0 讨论(0)
  • 2020-11-28 21:49

    I also had this issue using Lumen, but fixed by setting DB_STRICT_MODE=false in .env file.

    0 讨论(0)
  • 2020-11-28 21:54

    I also faced that problem and there are two ways to solve this in 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();
          });
      }
      

    as the above example, you can set nullable() for that feature. then when you are inserting data MySQL set the default value as null.

    1. 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.

    0 讨论(0)
  • 2020-11-28 21:58

    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!

    0 讨论(0)
  • 2020-11-28 21:59

    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 ''
    
    0 讨论(0)
提交回复
热议问题