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

Sqlite 在执行AutoMigration时候,如果tag中指定了索引名称,会导致不同表执行生成同样的索引名称然后报错 #7346

Open
lazychanger opened this issue Jan 16, 2025 · 2 comments
Assignees
Labels

Comments

@lazychanger
Copy link

lazychanger commented Jan 16, 2025

example: go-gorm/playground#782

package main

type Order struct {
	ID      uint64 `gorm:"column:id;type:bigint unsigned;primaryKey;autoIncrement:true"`
	UserID  uint64 `gorm:"column:user_id;type:bigint unsigned;not null;uniqueIndex:unique_user,priority:1;"`
	Product string `gorm:"column:product;type:varchar(63);not null;"`
}

type User struct {
	ID      uint64 `gorm:"column:id;type:bigint unsigned;primaryKey;autoIncrement:true"`
	UserID  uint64 `gorm:"column:user_id;type:bigint unsigned;not null;uniqueIndex:unique_user,priority:1;"`
	Name string `gorm:"column:product;type:varchar(63);not null;"`
}

func main() {
  	db, err := gorm.Open(sqlite.Open("gormtest.db"), &gorm.Config{})
	if err != nil {
		panic(errors.Wrapf(err, "failed to connect to database"))
	}
        
db.AutoMigrate(&Order{},&User{})
}
SQLERROR: SQL error or missing database (index unique_user already exists)

sqlite驱动需要在生成索引名称时候自动添加表前缀作为索引名称区别

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Jan 16, 2025
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

@github-actions github-actions bot added status:stale type:with reproduction steps with reproduction steps and removed type:missing reproduction steps missing reproduction steps labels Jan 16, 2025
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

2 participants