Environment
platform: macos go version: 1.16.6 gorm verison: v1.23.3
Description
When I execute it the second time, there is the wrong statement
ALTER TABLE `user` ADD `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键'
which already exists. And an error is reported: busy buffer
Code
package main
import (
"fmt"
"log"
"os"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type User struct {
ID uint64 `json:"id" gorm:"column:id;primaryKey;type:bigint(20) unsigned NOT NULL AUTO_INCREMENT;comment:主键"`
}
func (User) TableName() string {
return "user"
}
func main() {
dsn := "xxx"
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer(日志输出的目标,前缀和日志包含的内容——译者注)
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误
Colorful: false, // 禁用彩色打印
},
)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: newLogger,
})
if err != nil {
fmt.Println(err.Error())
}
tx := db.Begin()
err = tx.Set("gorm:table_options", "ENGINE=InnoDB").
Set("gorm:table_options", "CHARSET=utf8mb4").
AutoMigrate(
&User{},
)
if err != nil {
tx.Rollback()
}
}
Error log
``` 022/03/30 12:42:48 /Users/gloriahong/go/pkg/mod/gorm.io/driver/mysql@v1.3.2/migrator.go:228 [3.041ms] [rows:-] SELECT DATABASE()
2022/03/30 12:42:48 /Users/gloriahong/go/pkg/mod/gorm.io/driver/mysql@v1.3.2/migrator.go:231 [6.596ms] [rows:1] SELECT SCHEMA_NAME from Information_schema.SCHEMATA where SCHEMA_NAME LIKE 'test%' ORDER BY SCHEMA_NAME='test' DESC limit 1
2022/03/30 12:42:48 /Users/gloriahong/data/study/migrate/main.go:43 [4.392ms] [rows:-] SELECT count(*) FROM information_schema.tables WHERE table_schema = 'test' AND table_name = 'device' AND table_type = 'BASE TABLE'
2022/03/30 12:42:48 /Users/gloriahong/go/pkg/mod/gorm.io/driver/mysql@v1.3.2/migrator.go:228 [3.130ms] [rows:-] SELECT DATABASE()
2022/03/30 12:42:48 /Users/gloriahong/go/pkg/mod/gorm.io/driver/mysql@v1.3.2/migrator.go:231 [8.815ms] [rows:1] SELECT SCHEMA_NAME from Information_schema.SCHEMATA where SCHEMA_NAME LIKE 'test%' ORDER BY SCHEMA_NAME='test' DESC limit 1
2022/03/30 12:42:48 /Users/gloriahong/go/pkg/mod/gorm.io/driver/mysql@v1.3.2/migrator.go:148
[3.910ms] [rows:-] SELECT * FROM device LIMIT 1
[mysql] 2022/03/30 12:42:48 packets.go:447: busy buffer
[mysql] 2022/03/30 12:42:48 connection.go:173: bad connection
2022/03/30 12:42:48 /Users/gloriahong/go/pkg/mod/gorm.io/driver/mysql@v1.3.2/migrator.go:166 driver: bad connection [0.118ms] [rows:-] SELECT column_name, column_default, is_nullable = 'YES', data_type, character_maximum_length, column_type, column_key, extra, column_comment, numeric_precision, numeric_scale , datetime_precision FROM information_schema.columns WHERE table_schema = 'test' AND table_name = 'device' ORDER BY ORDINAL_POSITION [mysql] 2022/03/30 12:42:48 packets.go:447: busy buffer [mysql] 2022/03/30 12:42:48 packets.go:447: busy buffer [mysql] 2022/03/30 12:42:48 packets.go:428: busy buffer
2022/03/30 12:42:48 /Users/gloriahong/data/study/migrate/main.go:43 driver: bad connection
[0.017ms] [rows:0] ALTER TABLE device ADD id bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键'
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: a631807682
https://github.com/go-gorm/gorm/issues/5175
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨