i change to automigrate this struct:
type User struct {
ID uint
Name string
}
with this:
func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
fmt.Println("failed to connect database:", err)
return
}
// AutoMigrate the User model
err = db.AutoMigrate(&model.User{})
if err != nil {
fmt.Println("failed to migrate database:", err)
}
}
then i got err,it says i have multi primarykey with id:
0m[33m[0.010ms] [34;1m[rows:0][0m CREATE TABLE `users` (`id` integer PRIMARY KEY AUTOINCREMENT,`name` text,PRIMARY KEY (`id`))
failed to migrate database: table "users" has more than one primary key
i have tried delete the gorm tag,but it doesn't work,change ID to String type would solve this problem,or simply delete the ID line will make it work,but how could i keep this line? if i really want an ID colum in struct ,is there any other solution?
As you can check on the documentation of gorm over here,
https://gorm.io/docs/migration.html#:~:text=NOTE%3A%20AutoMigrate%20will%20create%20tables,columns%20to%20protect%20your%20data.
You can see that none of the columns are deleted which you migrate your structs, which causes such issues as the id column with different data type might already have been created.