Laravel - artisan -不能插入空列


Laravel - artisan - cannot insert with null column

我有这个表结构,我用laravel builder部分创建:

public function up() {
    DB::statement('
        CREATE TABLE `tbl_permission` (
          `permission_id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
          `id_module` smallint(5) unsigned NOT NULL,
          `name` varchar(50) NOT NULL,
          `create` TINYINT NOT NULL DEFAULT 0,
          `view` TINYINT NOT NULL DEFAULT 0,
          `is_composition` tinyint(1) NOT NULL DEFAULT "0", 
          `suffix_czech_name` VARCHAR(100), 
          `permission_table` VARCHAR(50),   
          FOREIGN KEY (`id_module`) REFERENCES `tbl_modules` (`id_module`)
        ) COLLATE utf8_czech_ci;
    ');
}

,然后我有另一个迁移插入:

public function up() {
    DB::table('tbl_permission')->insert([
        ['name' => 'account_bad_rooms', 'id_module' => 1, 'create' => 0, 'view' => 1, 'is_composition' => 0, 'suffix_czech_name' => 'name'],
        ['name' => 'account', 'id_module' => 1, 'create' => 1, 'view' => 1, 'is_composition' => 0, 'suffix_czech_name' => 'name'],
        ['name' => 'accountRoomIdConfig', 'id_module' => 1, 'create' => 1, 'view' => 1, 'is_composition' => 0, 'suffix_czech_name' => 'name', 'permission_table' => 'accountRoomIdConfig']
    ]);
}

当我使用迁移,然后一切工作没有任何错误,除了我没有任何插入的数据。我发现这是因为在两次插入中,我没有列permission_table可以为空。当我添加这一列时,那么所有的插入都具有相同的结构,迁移是好的。问题是我有超过70个插入,有些有列permission_table,有些没有。是否有可能插入没有相同结构的所有数据?

如果你做插入没有设置permission_table值,你会得到一个sql错误value list does not match column list。即使默认值为空。您仍然必须在所有行中传递值为"的列,即使它没有值。

public function up() {
DB::table('tbl_permission')->insert([
    [
     'name' => 'account_bad_rooms', 
     'id_module' => 1, 
     'create' => 0, 
     'view' => 1, 
     'is_composition' => 0, 
     'suffix_czech_name' => 'name'
     'permission_table' => ''
    ],
    [ 
      .... next record
    ]
  ]);
}

希望这有帮助。