Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoMigrate函数迁移sqlite数据库表失败 #7275

Open
viyonBox opened this issue Nov 12, 2024 · 5 comments
Open

AutoMigrate函数迁移sqlite数据库表失败 #7275

viyonBox opened this issue Nov 12, 2024 · 5 comments
Assignees
Labels

Comments

@viyonBox
Copy link

viyonBox commented Nov 12, 2024

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.

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Nov 12, 2024
Copy link

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.ioSearch Before Asking

@Scythemen
Copy link

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?

Copy link

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.ioSearch Before Asking

@viyonBox
Copy link
Author

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?

原始的数据库建表语句中本身就没有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函数是可以迁移成功的。

Copy link

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.ioSearch Before Asking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants