【问题标题】:Laravel - artisan - cannot insert with null columnLaravel - 工匠 - 无法插入空列
【发布时间】:2015-11-21 17:53:17
【问题描述】:

我有这个表结构,部分是用 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,有些没有。是否可以插入所有没有相同结构的数据?

【问题讨论】:

    标签: php mysql laravel laravel-5 laravel-artisan


    【解决方案1】:

    如果你在没有设置 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
        ]
    
      ]);
    }
    

    希望对您有所帮助。

    【讨论】:

    • 嗯,这就是我想要避免的,因为我有很多行。无论如何,谢谢。
    • @Draffix 没问题。
    猜你喜欢
    • 2014-03-26
    • 2021-06-15
    • 2021-06-12
    • 2016-12-06
    • 2017-05-05
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    相关资源
    最近更新 更多