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

ptrstring in left join not nil #781

Open
wants to merge 2 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
86 changes: 43 additions & 43 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,46 +122,46 @@ jobs:
- name: Tests
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']
platform: [ubuntu-latest] # can not run test in macOS and windows
runs-on: ${{ matrix.platform }}

services:
mssql:
image: mcmoe/mssqldocker:latest
env:
ACCEPT_EULA: Y
SA_PASSWORD: LoremIpsum86
MSSQL_DB: gorm
MSSQL_USER: gorm
MSSQL_PASSWORD: LoremIpsum86
ports:
- 9930:1433
options: >-
--health-cmd="/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P LoremIpsum86 -l 30 -Q \"SELECT 1\" || exit 1"
--health-start-period 10s
--health-interval 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=sqlserver GORM_DSN="sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm" ./test.sh
# sqlserver:
# needs: sqlite
# strategy:
# matrix:
# go: ['1.21']
# platform: [ubuntu-latest] # can not run test in macOS and windows
# runs-on: ${{ matrix.platform }}

# services:
# mssql:
# image: mcmoe/mssqldocker:latest
# env:
# ACCEPT_EULA: Y
# SA_PASSWORD: LoremIpsum86
# MSSQL_DB: gorm
# MSSQL_USER: gorm
# MSSQL_PASSWORD: LoremIpsum86
# ports:
# - 9930:1433
# options: >-
# --health-cmd="/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P LoremIpsum86 -l 30 -Q \"SELECT 1\" || exit 1"
# --health-start-period 10s
# --health-interval 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=sqlserver GORM_DSN="sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm" ./test.sh
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func OpenTestConnection() (db *gorm.DB, err error) {

func RunMigrations() {
var err error
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}}
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}, &A{}, &B{}, &C{}}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })

Expand Down
32 changes: 29 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"testing"
)

Expand All @@ -9,12 +10,37 @@ import (
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
user := A{Name: "jinzhu"}

DB.Create(&user)

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
type AB struct {
A
*B
}
a, b, c := &A{}, &B{}, &C{}
aTable, bTable, cTable := a.TableName(), b.TableName(), c.TableName()
var result1 AB
err := DB.Model(a).Select(aTable+".*, "+bTable+".*").Joins(
"LEFT JOIN "+bTable+" ON "+aTable+".id = "+bTable+".a_id",
).Where(aTable+".id = ?", user.ID).Scan(&result1).Error
if err != nil {
t.Errorf("Failed, got error: %v", err)
}
fmt.Println("This result is in line with expectations: ")
fmt.Println("no match b and b is nil: ", result1)

type AC struct {
A
*C
}
var result2 AC
err = DB.Model(a).Select(aTable+".*, "+cTable+".*").Joins(
"LEFT JOIN "+cTable+" ON "+aTable+".id = "+cTable+".a_id",
).Where(aTable+".id = ?", user.ID).Scan(&result2).Error
if err != nil {
t.Errorf("Failed, got error: %v", err)
}
fmt.Println("This result is not as expected: ")
fmt.Println("no match c but c is not nil: ", result2)
}
29 changes: 29 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,32 @@ type Language struct {
Code string `gorm:"primarykey"`
Name string
}

type A struct {
gorm.Model
Name string
}

func (A) TableName() string {
return "a"
}

type B struct {
gorm.Model
AID uint `gorm:"a_id"`
Name string
}

func (B) TableName() string {
return "b"
}

type C struct {
gorm.Model
AID uint `gorm:"a_id"`
Name *string
}

func (C) TableName() string {
return "c"
}
Loading