当前位置:网站首页>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
边栏推荐
- 论文写作 19: 会议论文与期刊论文的区别
- Electron入门教程3 ——进程通信
- R language ggplot2 visualization: ggplot2 visualizes the scatter diagram and uses geom_ mark_ The ellipse function adds ellipses around data points of data clusters or data groups for annotation
- Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
- PCL点云处理之直线与平面的交点计算(五十三)
- STM32基础知识
- 如何做产品创新?——产品创新方法论探索一
- NC basic usage
- Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
- 腾讯邱东洋:深度模型推理加速的术与道
猜你喜欢
波场DAO新物种下场,USDD如何破局稳定币市场?
DTMF dual tone multi frequency signal simulation demonstration system
PCL点云处理之计算两平面交线(五十一)
CVPR 2022 | QueryDet:使用级联稀疏query加速高分辨率下的小目标检测
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
Livego + ffmpeg + RTMP + flvjs to realize live video
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
Fundamentals of programming language (2)
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
随机推荐
Redis的安装(CentOS7命令行安装)
R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行BoxCox变换处理(将非正态分布数据列转换为正态分布数据、不可以处理负数)、设置method参数为BoxCox
Electron入门教程4 —— 切换应用的主题
Understanding various team patterns in scrum patterns
Mysql database backup scheme
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
腾讯邱东洋:深度模型推理加速的术与道
Five minutes to show you what JWT is
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
使用 WPAD/PAC 和 JScript在win11中进行远程代码执行1
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
How to create bep-20 pass on BNB chain
MySQL advanced lock - overview of MySQL locks and classification of MySQL locks: global lock (data backup), table level lock (table shared read lock, table exclusive write lock, metadata lock and inte
Numpy sort search count set
程序设计语言基础(2)
R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
中金财富公司怎么样,开户安全吗
Servlet learning notes