【严重】当修改结构体主键comment时,会意外丢失主键的自增属性 #2444
Labels
No Label
backport/done
backport/v1
blocked
db
oracle
db
sqlserver
duplicate
feature
cache
frontport/done
frontport/main
invalid
kind
breaking
kind
bug
kind
build
kind
dependencies
kind
docs
kind
driver
kind
enhancement
kind
feature
kind
performance
kind
proposal
kind
question
kind
refactor
kind
testing
need
feedback
need
test
proposal:accepted
RaspBerry Pi
regression
skip-changelog
upstream
wip
wontfix
No Milestone
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: xorm/xorm#2444
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
我们的项目使用了xorm,数据库使用的是Mysql。近日发现,某些表格的主键列的自增属性会莫名消失,查看xorm源码后,发现有bug。
测试:
数据库:mysql Ver 8.0.36
xorm:v1.3.9
定义结构体:
使用Sync2来同步表结构
user表不存在时,当进程启动时,xorm会自动创建user表
此时表结构为
主键id是自增属性的
当我们给id加一下comment,
结构体变为
重新启动进程,xorm触发Alter table指令
注意xorm执行的是
这句话没有带设置AUTO_INCREMENT属性,等于删除了主键的AUTO_INCREMENT!
此时表结构更改为
正确的修改指令应为
源码分析
从源码的角度分析,BUG来自:https://gitea.com/xorm/xorm/src/tag/v1.3.9/dialects/mysql.go#L403
ColumnString()返回的SQL string 是不带AUTO_INCREMENT ,至于为什么不带,看看ColumnString的定义:https://gitea.com/xorm/xorm/src/tag/v1.3.9/dialects/dialect.go#L300
其中有一段是这么写的,传进去的includePrimaryKey 是false所以AUTO_INCREMENT 是一定不会写到字符串返回的。
总的逻辑:当结构体的主键comment跟数据库表的comment不一致时,会触发修改数据库表主键属性的逻辑,调用了ModifyColumnSQL,而生成SQL语句的ColumnString没有将自增属性也写到SQL中,导致最后出来的SQL语句是不带自增属性的
要修复的话,大概就是这么改