Skip to content

Commit

Permalink
Wrong references when embedded struct has the same field
Browse files Browse the repository at this point in the history
  • Loading branch information
surik committed Aug 9, 2023
1 parent 3f14b65 commit 597832b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 57 deletions.
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{}{&Account{}, &Peer{}}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })

Expand Down
23 changes: 19 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@ package main

import (
"testing"

"gorm.io/gorm"
)

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

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
account := Account{
ID: "account1",
Network: Network{
ID: "network1",
},
Peers: []Peer{
{
ID: "peer1",
},
},
}

DB.Create(&user)
DB.Session(&gorm.Session{FullSaveAssociations: true}).Create(account)

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
var peer Peer
if err := DB.First(&peer, "id = ?", account.Peers[0].ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
if peer.AccountID != account.ID {
t.Error("account id of the peer doesn't match account id")
}
}
61 changes: 9 additions & 52 deletions models.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,17 @@
package main

import (
"database/sql"
"time"

"gorm.io/gorm"
)

// User has one `Account` (has one), many `Pets` (has many) and `Toys` (has many - polymorphic)
// 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 User struct {
gorm.Model
Name string
Age uint
Birthday *time.Time
Account Account
Pets []*Pet
Toys []Toy `gorm:"polymorphic:Owner"`
CompanyID *int
Company Company
ManagerID *uint
Manager *User
Team []User `gorm:"foreignkey:ManagerID"`
Languages []Language `gorm:"many2many:UserSpeak"`
Friends []*User `gorm:"many2many:user_friends"`
Active bool
}

type Account struct {
gorm.Model
UserID sql.NullInt64
Number string
}

type Pet struct {
gorm.Model
UserID *uint
Name string
Toy Toy `gorm:"polymorphic:Owner;"`
}

type Toy struct {
gorm.Model
Name string
OwnerID string
OwnerType string
ID string `gorm:"primaryKey"`
Network Network `gorm:"embedded;embeddedPrefix:network_"`
Peers []Peer `json:"-" gorm:"foreignKey:AccountID;references:ID"`
}

type Company struct {
ID int
Name string
type Network struct {
ID string
AccountID string
}

type Language struct {
Code string `gorm:"primarykey"`
Name string
type Peer struct {
ID string `gorm:"primaryKey"`
AccountID string
}

0 comments on commit 597832b

Please sign in to comment.