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

bug: count clear Joins #736

Closed
wants to merge 1 commit into from
Closed
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
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{}}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })

Expand Down
21 changes: 0 additions & 21 deletions gen.go

This file was deleted.

11 changes: 2 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ require (
gorm.io/driver/postgres v1.5.2
gorm.io/driver/sqlite v1.5.3
gorm.io/driver/sqlserver v1.5.1
gorm.io/gen v0.3.25
gorm.io/gorm v1.25.4
gorm.io/gorm v1.25.10
)

require (
Expand All @@ -23,13 +22,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.5.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/tools v0.15.0 // 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
)

replace gorm.io/gorm => ./gorm
replace gorm.io/gorm => ./gorm
49 changes: 47 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,52 @@
package main

import "fmt"
import (
"fmt"
"log"
)

type AB struct {
Id int `json:"id" gorm:"primaryKey"`
BId int `json:"aId" gorm:"index"`
AId int `json:"bId" gorm:"index"`
B B `json:"b"`
A A `json:"a"`
}

type A struct {
Id int `json:"id" gorm:"primaryKey"`
Name string `json:"name" gorm:"uniqueIndex"`
}

type B struct {
Id int `json:"id" gorm:"primaryKey"`
Name string `json:"name" gorm:"uniqueIndex"`
}

func main() {
fmt.Println("vim-go")

user := User{
Account: Account{
Number: "123456",
Companies: []Company{
{Name: "Corp1"}, {Name: "Corp2"},
},
Pet: Pet{
Name: "Pet1",
},
},
}
DB.AutoMigrate(&User{}, &Account{}, &Pet{}, &Company{})
DB.Create(&user)
fmt.Println("-------------------------------------------------------")
var count int64
var result User
DB = DB.Model(&User{}).
Joins("Account").
Joins("Account.Pet").
Preload("Account.Companies")

log.Println(count)
log.Println(result)

}
34 changes: 32 additions & 2 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,41 @@ import (
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

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

DB.Create(&user)
user := User{
Account: Account{
Number: "123456",
Companies: []Company{
{Name: "Corp1"}, {Name: "Corp2"},
},
Pet: Pet{
Name: "Pet1",
},
},
}

DB.Create(&user)
fmt.Println("-------------------------------------------------------")
var count int64
var result User
DB = DB.Model(&User{}).
Joins("Account").
Joins("Account.Pet").
Preload("Account.Companies")

if err := DB.Count(&count).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}

if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}

if len(result.Account.Companies) != 2 {
t.Errorf("Failed, got %v", len(result.Account.Companies))
}

if result.Account.Pet.Name != "Pet1" {
t.Errorf("Failed, got '%v'", result.Account.Pet.Name)
}
}
43 changes: 10 additions & 33 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package main

import (
"database/sql"
"time"

"gorm.io/gorm"
)

Expand All @@ -13,48 +11,27 @@ import (
// 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
Account Account
}

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

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

type Toy struct {
gorm.Model
type Company struct {
ID int
AccountID int32
Name string
OwnerID string
OwnerType string
}

type Company struct {
ID int
Name string
}
type Pet struct {
gorm.Model

type Language struct {
Code string `gorm:"primarykey"`
Name string
AccountID *uint
Name string
}
Loading