当前位置:网站首页>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
边栏推荐
- Mysql database and table building: the difference between utf8 and utf8mb4
- STM32 Basics
- Remote code execution in Win 11 using wpad / PAC and JScript
- SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
- Why does ES6 need to introduce map when JS already has object type
- Investigate why close is required after sqlsession is used in mybatties
- Mysql database - single table query (II)
- Electron入门教程3 ——进程通信
- NC basic usage 3
- Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
猜你喜欢

Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (V)

Redis cache penetration, cache breakdown, cache avalanche

SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions

An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue

Unity general steps for creating a hyper realistic 3D scene

Compact CUDA tutorial - CUDA driver API

Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
![[target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code](/img/30/39de1628cb47f715056fb898955ff5.png)
[target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code

PHP reference manual string (7.2000 words)

The textarea cursor cannot be controlled by the keyboard due to antd dropdown + modal + textarea
随机推荐
The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
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
. Ren -- the intimate artifact in the field of vertical Recruitment!
Design of library management database system
PCL点云处理之基于PCA的几何形状特征计算(五十二)
R语言使用timeROC包计算无竞争风险情况下的生存资料多时间AUC值、使用confint函数计算无竞争风险情况下的生存资料多时间AUC指标的置信区间值
nc基础用法1
NC basic usage 2
An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
Mysql database - connection query
Building googlenet neural network based on pytorch for flower recognition
Why does ES6 need to introduce map when JS already has object type
[2022] regard 3D target detection as sequence prediction - point2seq: detecting 3D objects as sequences
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
Grafana shares links with variable parameters
Remote code execution in Win 11 using wpad / PAC and JScript 1
Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?
记录:调用mapper报空指针;<foreach>不去重的用法;
Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration