From df9f67fc0338540f067174d1f170fe595536703d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=BE=E4=B8=80=E9=A5=BC?= Date: Thu, 16 May 2024 17:43:32 +0800 Subject: [PATCH] bug: count clear Joins --- main.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 50e8d8d3..e938bd5b 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,35 @@ package main -import "fmt" +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") + + DB.Migrator().DropTable(&AB{}, &A{}, &B{}) + DB.Migrator().CreateTable(&AB{}, &A{}, &B{}) + var ( + count int64 + appNodes = []*AB{} + ) + db := DB.Model(&AB{}).Preload("A").Joins("B").Where(`id = ?`, 11) + + db.Count(&count) + + db.Limit(1).Offset(0).Order("id DESC").Find(&appNodes) + }