“gorm gen database to struct”中JSON类型的映射,如果不传值会导致创建时出错

问题描述 投票:0回答:0

桌子:

type SQL
CREATE TABLE `user_profile` (
  `uuid` varchar(50) NOT NULL UNIQUE COMMENT 'uuid',
  `user_id` int NOT NULL COMMENT 'user id',
  `nickname` varchar(20) NOT NULL DEFAULT '' COMMENT 'nickname',
  `age` tinyint NOT NULL DEFAULT 0 COMMENT 'age',
  `active` bool NOT NULL DEFAULT true COMMENT 'whether the user is active',
  `rank` float NOT NULL DEFAULT 0 COMMENT 'user rank',
  `other` json COMMENT 'other personal information',
  `card` json COMMENT 'introduction card',
  `state` enum('on', 'off') NOT NULL DEFAULT 'on' COMMENT 'blacklist: off - on',
  `is_del` tinyint NOT NULL DEFAULT 0 COMMENT 'whether the record is deleted',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'the time when the record is created',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'the time when the record is updated',
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `uk_uuid` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='user profile table';

错误:

错误 3140 (22032):无效的 JSON 文本:“文档为空。”在 在“user_profile.other”列的值中位置 0。

“我使用gorm gen database to struct命令将数据库映射到Golang structs,但是官方文档并没有很好的解决问题,所以希望有人能提供解决方案。”

mysql go go-gorm
© www.soinside.com 2019 - 2024. All rights reserved.