当前位置:网站首页>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语言ggplot2可视化分面图(facet_wrap)、使用lineheight参数自定义设置分面图标签栏(灰色标签栏)的高度
- Redis installation (centos7 command line installation)
- selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
- Mysql database - single table query (II)
- JDBC database addition, deletion, query and modification tool class
- Numpy Index & slice & iteration
- How about CICC fortune? Is it safe to open an account
- 使用 WPAD/PAC 和 JScript在win11中进行远程代码执行
- Numpy - creation of data type and array
- Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
猜你喜欢
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
Building googlenet neural network based on pytorch for flower recognition
STM32 Basics
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
程序设计语言基础(2)
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
LeetCode异或运算
Numpy mathematical function & logical function
随机推荐
nc基础用法2
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
How to create bep-20 pass on BNB chain
JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
PostgreSQL basic functions
2022 - Data Warehouse - [time dimension table] - year, week and holiday
Computing the intersection of two planes in PCL point cloud processing (51)
PCA based geometric feature calculation of PCL point cloud processing (52)
SRS deployment
The R language uses the timeroc package to calculate the multi time AUC value of survival data without competitive risk, and uses the confint function to calculate the confidence interval value of mul
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
微信中金财富高端专区安全吗,证券如何开户呢
R language ggplot2 visual facet_wrap, and use the lineheight parameter to customize the height of the facet icon tab (gray label bar)
Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
Numpy - creation of data type and array
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
中金财富公司怎么样,开户安全吗
R语言使用timeROC包计算存在竞争风险情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、R语言使用timeROC包的plotAUCcurve函数可视化多时间生存资料的AUC曲线