2017-05-09 106 views
0

奇怪的是 - 所有這些工作都在5.2,但我不知道爲了做到這一點而可能發生了什麼變化。下面是錯誤和正在插入的數組。Laravel 5.4升級 - 違反完整性約束 - 列不能爲空

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, [email protected], , , 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" => "[email protected]" 
    "description" => null 
    "gender" => null 
    "background_check_status" => "0" 
    "picture_url" => null 
    "work" => null 
    "position" => null 
    "country" => null 
    "location" => null 
    "hobbies" => null 
] 

這是打破了許多不同的領域遍佈整個網站,但他們都沒有打破過。 Laravel改變了這一點?

+1

確保性別列在數據庫中可以爲空。 – FrankerZ

+0

任何遷移文件..?我們想要看一眼。另外,你可以檢查表嗎..它是否允許爲null .. Laravel不應該引入這樣的破壞代碼..因爲保存db處理的東西.. Laravel只會將它傳遞給db。 –

回答

5

假設沒有在你的代碼改變了我能想到的唯一的事情就是5.4推出了兩個新的中間件:TrimStringsConvertEmptyStringsToNull

儘量發表意見,後者或兩者app\Http\Kernel.php

class Kernel extends HttpKernel 
{ 
    protected $middleware = [ 
     \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, 
     \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, 
     \App\Http\Middleware\TrimStrings::class, 
     // \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, 
    ]; 

    // ... 
} 
+0

'ConvertEmptyStringsToNull'很可能是罪魁禍首。第一個字符串傳遞給數據庫,現在他們試圖傳遞NULL。 – FrankerZ

+0

你是一個拯救生命的人,從字面上來說花了好幾個小時,因爲這是一個巨大的混亂體驗。謝謝。 –

+0

很高興幫助:) – peterm

1

繼續上@ peterm的anwser。

如果您仍然希望將空字符串轉換爲null(用於應用程序的其他部分)並且不想取消註釋\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,那麼您有兩種選擇。

  1. nullable添加到您的數據庫列中。
  2. 使用eventssaving在您的模型上爲您的財產添加默認值,如果它是null

下面是關於如何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'; 
     }); 
    } 
} 
相關問題