duan5362 2015-08-27 11:12
浏览 44
已采纳

Laravel - artisan - 无法插入空列

I have this table structure which I created partly with 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;
    ');
}

then I have another migration with insert:

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']
    ]);
}

When I use migration then everything works without any error except I don't have any inserted data. I figured out that is because in two inserts I don't have column permission_table which can be null. When I add this column, then all inserts have same structure, migration is fine. Problem is that I have more than 70 inserts and some have column permission_table and some does not have it. Is somehow possible to insert all data without same structure?

  • 写回答

1条回答 默认 最新

  • dswfyq6201 2015-08-27 15:02
    关注

    If you do an insert without setting permission_table value you will get a sql error value list does not match column list. Even if default value is null. you still have to pass the column in all rows with a value of '' even if it does not have a value.

    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
        ]
    
      ]);
    }
    

    Hope this help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100