GORM Playground Link
https://github.com/go-gorm/gorm/releases/tag/v1.25.12
Description
1.已经存在了一个sqlite3数据库,表结构已经通过sql语句被建立好了,建表语句如下:
CREATE TABLE tb_levels (
-- level id is a unique id of a level, please ensure this.
level_id INTEGER NOT NULL,
-- Duplicate names are allowed under the same group_id.
name TEXT NOT NULL,
code INTEGER NOT NULL,
-- 0 or 1, 0-predefine 1-customize.
source INTEGER NULL DEFAULT 1
);
生成一个a.db的sqlite数据库文件。
2.使用AutoMigrate函数修改表结构,go代码程序如下:
package main
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type TbLevels struct {
LevelId uint64 `gorm:"level_id"`
Name string `gorm:"name"`
Code string `gorm:"code"`
Source int32 `gorm:"source"`
GroupID int `gorm:"group_uuid"`
}
func main() {
dbPath := "./a.db"
dbHandle, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
if err != nil {
fmt.Printf("Failed to open %s, error is %s.\n", dbPath, err.Error())
return
}
err = dbHandle.AutoMigrate(&TbLevels{})
if err != nil {
fmt.Printf("Failed to auto migrate database, error is %s, retry.\n", err.Error())
} else {
fmt.Printf("Migrate database successfully.")
}
}
go build 生成gormtest程序 3.运行gormtest程序,报错如下 ./gormtest
2024/11/12 11:41:54 table tb_levels__temp has no column named please
[0.017ms] [rows:0] INSERT INTO tb_levels__temp(please,code,0) SELECT please,code,0 FROM tb_levels
Failed to auto migrate database, error is table tb_levels__temp has no column named please, retry.
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: Scythemen
There is no column named please.
Your defined struct with name TbLevels, and the created table by sql with table name tb_levels, none of them has a column named please, just exactly what the error shows up.
Why don't you read the error more carefully?
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: viyonBox
There is no column named
please. Your defined struct with nameTbLevels, and the created table by sql with table nametb_levels, none of them has a column namedplease, just exactly what the error shows up.Why don't you read the error more carefully?
原始的数据库建表语句中本身就没有please这一列。我的理解是AutoMigrate内部在解析数据库建表语句的时候,用的逗号分割的schema,导致把please误认为是一个column,而实际上不是。
如果建表语句是下面这样
CREATE TABLE tb_levels (
level_id INTEGER NOT NULL,
name TEXT NOT NULL,
code INTEGER NOT NULL,
source INTEGER NULL DEFAULT 1
);
AutoMigrate函数是可以迁移成功的。
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 ✨