在開發和設計資料庫模型時,有時我們需要查詢關係中的嵌入值是否等於特定值。這個問題在實際應用中經常遇到,但解決起來可能並不容易。在本文中,我將向大家介紹一個有效的方法來查詢關係的嵌入值是否等於特定值的模型。無需擔心,我會用簡潔易懂的語言解釋清楚,幫助您快速理解並應用到實際開發中。讓我們一起來看看吧!
問題內容
我有兩個不同的模型(cars
和types
),它們彼此相關(屬於關係),其中兩個模型都有一個嵌入式struct
用於公共資料(post
)。我想檢索某些 types
,但只想收到 post
的 cars
值等於某個值的答案。
shorty說,根據下面的模型,我想找到所有types
,其中cars.post.published
等於true。
模型
type post struct { published bool } type car struct { gorm.model brand string post post `gorm:"embedded"` } type type struct { gorm.model name string carid uint32 car car post post `gorm:"embedded"` }
使用 db.preload("car").find(&type)
我能夠在答案物件中取得 car
值。如果我在car
結構上使用where()
函數(即where(car{brand: "volvo"}
) 我可以透過brand
取得值,但當使用post
時(即where(car{post: post {published: true})
) 它只會傳回所有內容。
我最好使用需要查詢的主模型作為 where()
函數的基礎。例如:
q := Type{Car: Car{Post: Post{Published: true}}} db.Preload("Car").Where(q).Find(&Type)
...但這似乎不起作用。如何在不使用原始 sql 生成器的情況下實現這樣的查詢?
解決方法
我能夠透過以下方式解決您的問題。首先,我將分享程式碼,然後我將介紹值得解釋的要點。
package main import ( "fmt" "gorm.io/driver/postgres" "gorm.io/gorm" ) type Post struct { Published bool } type Car struct { gorm.Model Brand string TypeID int Type Type Post Post `gorm:"embedded"` } type Type struct { gorm.Model Name string CarID int Post Post `gorm:"embedded"` } func main() { dsn := "host=localhost port=54322 user=postgres password=postgres dbname=postgres sslmode=disable" db, err := gorm.Open(postgres.Open(dsn)) if err != nil { panic(err) } db.AutoMigrate(&Car{}) db.AutoMigrate(&Type{}) // uncomment these to seed data // db.Create(&Car{Brand: "Tesla", Type: Type{Name: "SUV", Post: Post{Published: true}}, Post: Post{Published: true}}) // db.Create(&Car{Brand: "Ford", Type: Type{Name: "City", Post: Post{Published: false}}, Post: Post{Published: false}}) var cars []Car if err := db.Debug().Model(&Car{}).Preload("Type").Where(&Car{Post: Post{Published: true}}).Find(&cars).Error; err != nil { panic(err) } for _, v := range cars { fmt.Println(v.Type.Name) } }
現在,讓我分享一些見解。
結構體定義
我稍微改變了它來處理這個場景。我從 type
結構中刪除了 car
字段,並在 car
結構定義中添加了其對應項。
設定與種子
然後,我透過 gorm 設定了資料庫連接。我將程式碼中定義的模型與資料庫中存在的關係同步。為了演示,我手動播種了一些虛擬資料。
讀取邏輯
然後,我執行查詢來取得相關資料。我使用了以下方法:
-
debug
:用來記錄實際的sql語句 -
model
:用來指定我們要處理的關係 -
preload
:用於載入type
關聯 -
where
:用於指定條件(在我們的例子中,過濾器位於嵌入結構上) -
find
:用於將結果對應到變數
請告訴我這是否有助於解決您的問題,謝謝!
以上是如何查詢關係的嵌入值等於特定值的模型?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Golang和Python的主要區別在於並發模型、類型系統、性能和執行速度。 1.Golang使用CSP模型,適用於高並發任務;Python依賴多線程和GIL,適合I/O密集型任務。 2.Golang是靜態類型,Python是動態類型。 3.Golang編譯型語言執行速度快,Python解釋型語言開發速度快。

Golang通常比C 慢,但Golang在並發編程和開發效率上更具優勢:1)Golang的垃圾回收和並發模型使其在高並發場景下表現出色;2)C 通過手動內存管理和硬件優化獲得更高性能,但開發複雜度較高。

Golang在雲計算和DevOps中的應用廣泛,其優勢在於簡單性、高效性和並發編程能力。 1)在雲計算中,Golang通過goroutine和channel機制高效處理並發請求。 2)在DevOps中,Golang的快速編譯和跨平台特性使其成為自動化工具的首選。

Golang和C 在執行效率上的表現各有優勢。 1)Golang通過goroutine和垃圾回收提高效率,但可能引入暫停時間。 2)C 通過手動內存管理和優化實現高性能,但開發者需處理內存洩漏等問題。選擇時需考慮項目需求和團隊技術棧。

Golang更適合高並發任務,而Python在靈活性上更有優勢。 1.Golang通過goroutine和channel高效處理並發。 2.Python依賴threading和asyncio,受GIL影響,但提供多種並發方式。選擇應基於具體需求。

Golang和C 在性能上的差異主要體現在內存管理、編譯優化和運行時效率等方面。 1)Golang的垃圾回收機制方便但可能影響性能,2)C 的手動內存管理和編譯器優化在遞歸計算中表現更為高效。

selectgolangforhighpperformanceandcorrency,ifealforBackendServicesSandNetwork程序; selectpypypythonforrapiddevelopment,dataScience和machinelearningDuetoitsverserverserverserversator versator anderticality andextility andextentensivelibraries。

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。Golang以其并发模型和高效性能著称,Python则以简洁语法和丰富库生态系统著称。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

WebStorm Mac版
好用的JavaScript開發工具