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

Join with preload does not work as intended #748

Open
wants to merge 1 commit 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
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module gorm.io/playground
go 1.20

require (
github.com/stretchr/testify v1.8.1
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.2
gorm.io/driver/sqlite v1.5.3
Expand All @@ -12,6 +13,7 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
Expand All @@ -20,13 +22,17 @@ require (
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.5.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c // indirect
gorm.io/hints v1.1.0 // indirect
gorm.io/plugin/dbresolver v1.5.0 // indirect
Expand Down
130 changes: 125 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,139 @@ package main

import (
"testing"

"gorm.io/gorm/clause"

"github.com/stretchr/testify/assert"
)

// GORM_REPO: https://github.com/go-gorm/gorm.git
// GORM_BRANCH: master
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

type M1 struct {
ID string `gorm:"column:id; primaryKey; not null"`

M2 *M2
M2ID *string `gorm:"column:m2_id"`
}

type M2 struct {
ID string `gorm:"column:id; primaryKey; not null"`

M1 *M1
M1ID *string `gorm:"column:m1_id"`
}

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
DB.Migrator().DropTable(&M1{}, &M2{})
DB.Migrator().AutoMigrate(&M1{}, &M2{})

// Create

m1 := M1{
ID: "m1",
}
m2 := M2{
ID: "m2",
}

assert.NoError(t, DB.Create(&m1).Error)
assert.NoError(t, DB.Create(&m2).Error)

// Set foreign keys
m1.M2ID = &m2.ID
m2.M1ID = &m1.ID
assert.NoError(t, DB.Model(&m1).Updates(&m1).Error)
assert.NoError(t, DB.Model(&m2).Updates(&m2).Error)
m1.M2 = &m2
m2.M1 = &m1

var findM1 M1
assert.NoError(t, DB.First(&findM1, M1{ID: m1.ID}).Error)
assert.Equal(t, findM1.ID, m1.ID)
assert.Equal(t, *findM1.M2ID, m2.ID)
assert.Nil(t, findM1.M2) // No preload

DB.Create(&user)
var findM2 M2
assert.NoError(t, DB.First(&findM2, M2{ID: m2.ID}).Error)
assert.Equal(t, findM2.ID, m2.ID)
assert.Equal(t, *findM2.M1ID, m1.ID)
assert.Nil(t, findM2.M1) // No preload

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
// Now using preload all

assert.NoError(t, DB.Preload(clause.Associations).First(&findM1, M1{ID: m1.ID}).Error)
assert.Equal(t, findM1.ID, m1.ID)
assert.Equal(t, *findM1.M2ID, m2.ID)
assert.Equal(t, findM1.M2.ID, m2.ID) // With preload

assert.NoError(t, DB.Preload(clause.Associations).First(&findM2, M2{ID: m2.ID}).Error)
assert.Equal(t, findM2.ID, m2.ID)
assert.Equal(t, *findM2.M1ID, m1.ID)
assert.Equal(t, findM2.M1.ID, m1.ID) // With preload

// Break things by adding new database entries
{
m1New := M1{
ID: "m1-new",
}
m2New := &M2{
ID: "m2-new",
}

assert.NoError(t, DB.Create(&m1New).Error)
assert.NoError(t, DB.Create(&m2New).Error)

// Set foreign keys
m1New.M2ID = &m2.ID
m2New.M1ID = &m1.ID
assert.NoError(t, DB.Model(&m1New).Updates(&m1New).Error)
assert.NoError(t, DB.Model(&m2New).Updates(&m2New).Error)
m1New.M2 = &m2
m2New.M1 = &m1
}

// Now the same query as above yields different results

assert.NoError(t, DB.Preload(clause.Associations).First(&findM1, M1{ID: m1.ID}).Error)
assert.Equal(t, findM1.ID, m1.ID)
assert.Equal(t, *findM1.M2ID, m2.ID)
assert.Equal(t, findM1.M2.ID, m2.ID) // With preload

assert.NoError(t, DB.Preload(clause.Associations).First(&findM2, M2{ID: m2.ID}).Error)
assert.Equal(t, findM2.ID, m2.ID)
assert.Equal(t, *findM2.M1ID, m1.ID)
assert.Equal(t, findM2.M1.ID, m1.ID) // With preload

// Optional: Break even more things
{
m1New2 := M1{
ID: "m1-new2",
}
m2New2 := &M2{
ID: "m2-new2",
}

assert.NoError(t, DB.Create(&m1New2).Error)
assert.NoError(t, DB.Create(&m2New2).Error)

// Set foreign keys
m1New2.M2ID = &m2.ID
m2New2.M1ID = &m1.ID
m1New2.M2 = &m2
m2New2.M1 = &m1
assert.NoError(t, DB.Model(&m1New2).Updates(&m1New2).Error)
assert.NoError(t, DB.Model(&m2New2).Updates(&m2New2).Error)
}

// As we not only set the foreign ID but also the foreign object before
// updating above, the find operation fails now (which is a bug).
// The two issued queries don't make sense due to the duplicate "id" condition:
// SELECT * FROM `m1` WHERE `m1`.`id` = "m1-new2" AND `m1`.`id` = "m1" ORDER BY `m1`.`id` LIMIT 1
// SELECT * FROM `m2` WHERE `m2`.`id` = "m2-new2" AND `m2`.`id` = "m2" ORDER BY `m2`.`id` LIMIT 1

assert.NoError(t, DB.First(&findM1, M1{ID: m1.ID}).Error)

assert.NoError(t, DB.First(&findM2, M2{ID: m2.ID}).Error)
}
Loading