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

Wrong Foreign Key AutoMigrate default value #758

Open
wants to merge 7 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
76 changes: 2 additions & 74 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,11 @@ on:
- 'gh-pages'

jobs:
# Label of the container job
sqlite:
strategy:
matrix:
go: ['1.21']
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}

steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: go mod pakcage cache
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}

- name: Tests
run: GORM_DIALECT=sqlite ./test.sh

mysql:
needs: sqlite
strategy:
matrix:
dbversion: ['mysql:latest'] # 'mysql:5.7', 'mysql:5.6'
go: ['1.21']
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}

services:
mysql:
image: ${{ matrix.dbversion }}
env:
MYSQL_DATABASE: gorm
MYSQL_USER: gorm
MYSQL_PASSWORD: gorm
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
ports:
- 9910:3306
options: >-
--health-cmd "mysqladmin ping -ugorm -pgorm"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10

steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: go mod pakcage cache
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}

- name: Tests
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
strategy:
matrix:
dbversion: ['postgres:latest'] # 'postgres:11', 'postgres:10'
go: ['1.21']
go: ['1.23']
platform: [ubuntu-latest] # can not run in macOS and widnowsOS
runs-on: ${{ matrix.platform }}

Expand Down Expand Up @@ -123,10 +52,9 @@ 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
strategy:
matrix:
go: ['1.21']
go: ['1.23']
platform: [ubuntu-latest] # can not run test in macOS and windows
runs-on: ${{ matrix.platform }}

Expand Down
6 changes: 1 addition & 5 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ func OpenTestConnection() (db *gorm.DB, err error) {
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{})
}

if debug := os.Getenv("DEBUG"); debug == "true" {
db.Logger = db.Logger.LogMode(logger.Info)
} else if debug == "false" {
db.Logger = db.Logger.LogMode(logger.Silent)
}
db.Logger = db.Logger.LogMode(logger.Info)

return
}
Expand Down
20 changes: 0 additions & 20 deletions gen.go
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
package main

import (
"gorm.io/gen"
"gorm.io/gen/examples/dal"
)

func generate() {
g := gen.NewGenerator(gen.Config{
OutPath: "./dal/query",
Mode: gen.WithDefaultQuery, /*WithQueryInterface, WithoutContext*/

WithUnitTest: true,
})
g.UseDB(dal.DB)

g.ApplyBasic(Company{}, Language{}) // Associations
g.ApplyBasic(g.GenerateModel("user"), g.GenerateModelAs("account", "AccountInfo"))

g.Execute()
}
30 changes: 21 additions & 9 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
// 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 User struct {
gorm.Model
ID uint `gorm:"primarykey;type:bigint generated by default as identity;default:(-)"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Name string
Age uint
Birthday *time.Time
Expand All @@ -30,20 +33,29 @@ type User struct {
}

type Account struct {
gorm.Model
UserID sql.NullInt64
Number string
ID uint `gorm:"primarykey;type:bigint generated by default as identity;default:(-)"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
UserID sql.NullInt64
Number string
}

type Pet struct {
gorm.Model
UserID *uint
Name string
Toy Toy `gorm:"polymorphic:Owner;"`
ID uint `gorm:"primarykey;type:bigint generated by default as identity;default:(-)"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
UserID *uint
Name string
Toy Toy `gorm:"polymorphic:Owner;"`
}

type Toy struct {
gorm.Model
ID uint `gorm:"primarykey;type:bigint generated by default as identity;default:(-)"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Name string
OwnerID string
OwnerType string
Expand Down
Loading