当前位置:网站首页>go-zero框架数据库方面避坑指南
go-zero框架数据库方面避坑指南
2022-04-23 20:18:00 【Deng_Xian_Sheng】
先说一个貌似不是问题的问题,zero框架生成的或者文档给出的源码中可能会缺少应该导入的包
如果你使用goland会自动导入;但如果是vscode或者其他编辑器需要注意这个问题
————————————————————————————————————————-——
下面接着扯zero框架数据库相关
zero在生成insert、update代码时默认排除这两个字段
create_time
update_time
但是,不包括delete_time
;通读生成后的代码你就会发现;insert代码中delete_time
作为一个必要的参数被嵌入在代码中;如果在传参的结构体中不填该字段;可能会得到Incorrect datetime value: '0000-00-00' for column 'delete_time' at row1
所以,需要排除这个字段,或者自己写insert代码
#排除该字段
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
另一个问题
对于特殊字段,要注意为null问题
#delete_time字段要允许为null,且默认为null
`delete_time` timestamp(3) NULL DEFAULT NULL,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
如果sql字段约束、类型不正确;生成的代码可能会有这种情况
DeleteTime time.Time `db:"deleted_at"`
CreateTime time.Time `db:"created_time"`
UpdateTime time.Time `db:"updated_time"`
它本应为
DeleteTime sql.NullTime `db:"deleted_at"`
CreateTime time.Time `db:"created_time"`
UpdateTime time.Time `db:"updated_time"`
如果后期需要修改某表的字段,生成的CURD代码中除了表的结构体;CURD的函数也需要修改;否则会出现一些问题。(因为CURD的函数对表的结构体耦合性比较高)
总体来讲zero重点还是在rpc,使rpc更好用;更简单。
对于数据库的封装一般情况,不要过度依赖。
版权声明
本文为[Deng_Xian_Sheng]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Deng_Xian_Sheng/article/details/123215405
边栏推荐
- R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
- DNS cloud school rising posture! Three advanced uses of authoritative DNS
- LeetCode动态规划训练营(1~5天)
- R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行BoxCox变换处理(将非正态分布数据列转换为正态分布数据、不可以处理负数)、设置method参数为BoxCox
- Computing the intersection of two planes in PCL point cloud processing (51)
- R language ggplot2 visual facet_wrap, and use the lineheight parameter to customize the height of the facet icon tab (gray label bar)
- R语言使用timeROC包计算无竞争风险情况下的生存资料多时间AUC值、使用confint函数计算无竞争风险情况下的生存资料多时间AUC指标的置信区间值
- PCA based geometric feature calculation of PCL point cloud processing (52)
- Redis installation (centos7 command line installation)
- nc基础用法4
猜你喜欢
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
Leetcode XOR operation
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(八)
Servlet learning notes
STM32基础知识
Fundamentals of programming language (2)
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Building googlenet neural network based on pytorch for flower recognition
网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
CVPR 2022 | QueryDet:使用级联稀疏query加速高分辨率下的小目标检测
随机推荐
2022 - Data Warehouse - [time dimension table] - year, week and holiday
【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
Is the wechat CICC wealth high-end zone safe? How to open an account for securities
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
R语言使用timeROC包计算无竞争风险情况下的生存资料多时间AUC值、使用confint函数计算无竞争风险情况下的生存资料多时间AUC指标的置信区间值
Operation of numpy array
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
Local call feign interface message 404
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Mysql database and table building: the difference between utf8 and utf8mb4
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
【2022】将3D目标检测看作序列预测-Point2Seq: Detecting 3D Objects as Sequences
使用 WPAD/PAC 和 JScript在win11中进行远程代码执行1
Leetcode XOR operation
CVPR 2022 | QueryDet:使用级联稀疏query加速高分辨率下的小目标检测
Computing the intersection of two planes in PCL point cloud processing (51)
R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行BoxCox变换处理(将非正态分布数据列转换为正态分布数据、不可以处理负数)、设置method参数为BoxCox
LeetCode动态规划训练营(1~5天)
本地调用feign接口报404