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 Fails to Update ENUM Fields in MySQL #787

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
run: GORM_DIALECT=sqlite ./test.sh

mysql:
needs: sqlite
# needs: sqlite
strategy:
matrix:
dbversion: ['mysql:latest'] # 'mysql:5.7', 'mysql:5.6'
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
run: GORM_ENABLE_CACHE=true GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True" ./test.sh

postgres:
needs: sqlite
# needs: sqlite
strategy:
matrix:
dbversion: ['postgres:latest'] # 'postgres:11', 'postgres:10'
Expand Down Expand Up @@ -123,7 +123,7 @@ jobs:
run: GORM_ENABLE_CACHE=true GORM_DIALECT=postgres GORM_DSN="user=gorm password=gorm dbname=gorm host=localhost port=9920 sslmode=disable TimeZone=Asia/Shanghai" ./test.sh

sqlserver:
needs: sqlite
# needs: sqlite
strategy:
matrix:
go: ['1.21']
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ We are using the following configuration run your code (GORM's latest master bra
We have prepared some structs with relationships in [https://github.com/go-gorm/playground/blob/master/models.go](https://github.com/go-gorm/playground/blob/master/models.go) that you can use for your tests

## Happy Hacking!

8 changes: 4 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func RunMigrations() {

DB.Migrator().DropTable("user_friends", "user_speaks")

if err = DB.Migrator().DropTable(allModels...); err != nil {
log.Printf("Failed to drop table, got error %v\n", err)
os.Exit(1)
}
// if err = DB.Migrator().DropTable(allModels...); err != nil {
// log.Printf("Failed to drop table, got error %v\n", err)
// os.Exit(1)
// }

if err = DB.AutoMigrate(allModels...); err != nil {
log.Printf("Failed to auto migrate, but got error %v\n", err)
Expand Down
6 changes: 5 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
user := User{Name: "jinzhu", UserType: ADMIN}
user2 := User{Name: "jinzhu2", UserType: USER}
user3 := User{Name: "jinzhu2", UserType: VIEWER}

DB.Create(&user)
DB.Create(&user2)
DB.Create(&user3)

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
Expand Down
9 changes: 9 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import (
// He works in a Company (belongs to), he has a Manager (belongs to - single-table), and also managed a Team (has many - single-table)
// He speaks many languages (many to many) and has many friends (many to many - single-table)
// His pet also has one Toy (has one - polymorphic)
type UserType string

const (
ADMIN UserType = "admin"
USER UserType = "user"
VIEWER UserType = "viewer"
)

type User struct {
gorm.Model
Name string
Expand All @@ -27,6 +35,7 @@ type User struct {
Languages []Language `gorm:"many2many:UserSpeak"`
Friends []*User `gorm:"many2many:user_friends"`
Active bool
UserType UserType `gorm:"type:enum('admin', 'user', 'viewer');default:'user'"`
}

type Account struct {
Expand Down
Loading