Laravel 5.4升级 - 完整性约束违规 - 列不能为空

时间:2017-05-09 03:35:51

标签: php mysql laravel laravel-5.4

奇怪的是 - 所有这一切都在5.2工作,但我不知道可能会发生什么变化才能实现这一目标。下面是错误和插入的数组。

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'gender' cannot be null (SQL: insert into `tenants` (`name`, `phone`, `email`, `description`, `gender`, `date_birth`, `background_check_status`, `picture_url`, `work`, `position`, `country`, `location`, `hobbies`, `updated_at`, `created_at`) values (Amadeo Levy Luna, 18065496549, amadeo.luna@ttu.edu, , , 2017-05-08 20:29:50, 0, , , , , , , 2017-05-08 20:29:50, 2017-05-08 20:29:50)) ◀"
array:13 [▼
  "_token" => "9HeacY4KskT5vpLPGCUTkzVxRcpcKMNjdob79aLs"
  "name" => "Amadeo Levy Luna"
  "phone" => "18065496549"
  "email" => "amadeo.luna@ttu.edu"
  "description" => null
  "gender" => null
  "background_check_status" => "0"
  "picture_url" => null
  "work" => null
  "position" => null
  "country" => null
  "location" => null
  "hobbies" => null
]

这在整个网站的许多不同领域都有所突破,但之前都没有破坏过。 Laravel改变了什么来创造这个?

2 个答案:

答案 0 :(得分:6)

假设代码中没有任何变化,我唯一能想到的就是5.4中引入的两个新中间件:@Html.DropDownListFor(model => model.MovieID, new SelectList((IEnumerable)ViewBag.MoviesList, "Value", "Text", Model.MovieID), new { @class = "form-control" }) TrimStrings

ConvertEmptyStringsToNull

中尝试对后者或两者进行评论
app\Http\Kernel.php

答案 1 :(得分:2)

继续关注@ peterm的anwser。

如果您仍想将空字符串转换为null(对于您的应用程序的其他部分)并且不想取消注释\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,则有两种选择。

  1. nullable添加到数据库中的列。
  2. 在您的模型上使用events(例如saving}为您的媒体资源添加默认值null
  3. 以下是有关如何将nullable添加到列中的示例(如果您已经创建了表格以及是否使用了mysql):

    <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class AddNullToColumns extends Migration {
        public function __construct() {
            $this->charset = config('database.connections.mysql.charset');
            $this->collate = config('database.connections.mysql.collation');
        }
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up() {
            // Change multiple columns on a table.
            DB::statement("ALTER TABLE `" . (new \App\User)->getTable() . "`
                CHANGE `phone` `phone` VARCHAR(255) CHARACTER SET {$this->charset} COLLATE {$this->collate} NULL DEFAULT NULL,
                CHANGE `address` `address` TEXT CHARACTER SET {$this->charset} COLLATE {$this->collate} NULL DEFAULT NULL,
                CHANGE `comment` `comment` TEXT CHARACTER SET {$this->charset} COLLATE {$this->collate} NULL DEFAULT NULL;");
    
            // Change one column on a table.
            DB::statement("ALTER TABLE `" . (new \App\Report)->getTable() . "` CHANGE `comment` `comment` TEXT CHARACTER SET {$this->charset}COLLATE {$this->collate}NULL DEFAULT NULL");
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down() {
            DB::statement("ALTER TABLE `" . (new \App\User)->getTable() . "`
                CHANGE `phone` `phone` VARCHAR(255) CHARACTER SET {$this->charset} COLLATE {$this->collate} NOT NULL,
                CHANGE `address` `address` TEXT CHARACTER SET {$this->charset} COLLATE {$this->collate} NOT NULL,
                CHANGE `comment` `comment` TEXT CHARACTER SET {$this->charset} COLLATE {$this->collate} NOT NULL;");
    
            DB::statement("ALTER TABLE `" . (new \App\Report)->getTable() . "` CHANGE `comment` `comment` TEXT CHARACTER SET {$this->charset}COLLATE {$this->collate}NOT NULL");
        }
    }
    

    以下是有关如何使用事件的示例:

    <?php
    
    namespace App;
    
    class User extends Authenticatable {
        public static function boot() {
            // When creating or updated the model.
            static::saving(function($model){
                // Use value of gender if available, if `null` use `unisex`.
                $model->gender = $model->gender ?: 'unisex';
            });
        }
    }