搜尋
首頁後端開發Golang在 Gorm 中實現與值數組的關係

在 Gorm 中实现与值数组的关系

在 Gorm 中,實作與值陣列的關係是一項非常有用的功能。 Gorm 是一個流行的 Go 語言 ORM(物件關聯映射)庫,它允許開發者使用 Go 語言來操作資料庫。與傳統的關係型資料庫不同,值數組是一種特殊的資料結構,它允許將多個值作為一個整體儲存在資料庫中的某個欄位中。這對於儲存一些複雜的資料結構或提高查詢效率都非常有幫助。在本文中,我將向大家介紹如何在 Gorm 中實現與值數組的關係,以及如何使用它來提升開發效率。

問題內容

我正在嘗試使用 go 和 gorm 實作發票應用程式的模型。我已經定義了發票結構,並希望包含來自單獨結構的發票行項目。

type invoice struct {
    base
    companyid  string `gorm:"not null"`
    company    company
    invoiceno  string     `gorm:"not null"`
    currency   string     `gorm:"not null;default:'gbp'"`
    total      float64    `gorm:"not null"`
    terms      int        `gorm:"not null;default:30"`
    issueddate time.time  `gorm:"not null"`
    duedate    time.time  `gorm:"not null"`
    paid       bool       `gorm:"not null"`
    lineitems  []lineitem `gorm:"foreignkey:invoiceid"`
    void       bool       `gorm:"default:false"`
}

這是我的 lineitem 結構。

type lineitem struct {
    base
    service     string `gorm:"not null"`
    description string `gorm:"not null;"`
    amount      float64
    count       int
    unitbase    string  `gorm:"not null;default:'days'"` // days or hours
    total       float64 `gorm:"not null"`
}

當我嘗試建立應用程式時,出現以下錯誤。

... got error invalid field found for struct github.com/repo/API/database/models.Invoice's field LineItems: define a valid foreign key for relations or implement the Valuer/Scanner interface

我的想法是,我可以定義一個有限的設定訂單項,可以從固定費率和說明中進行選擇,以限制重複。

我不確定我的處理方式是否正確。

所以我的問題是,是否可以透過這種方式包含關係模型中的項目陣列?

解決方法

根據您的列名稱,我可以想到三種方法來實現此目的:

  1. 使用預設語法(無 gorm:foreignkey

    type invoice struct {
         id     //this is the primary key, may also be in your base model
         lineitems []lineitem
         ..other fields
     }
    
     type lineitem struct {
         id          //primary key of lineitem
         invoiceid   //automatically this will be your foreign key
         ..other fields
     }
  2. 指定自訂外鍵(假設第二個結構具有不同的列名稱,您希望將其作為外鍵)

    type invoice struct {
         id     //this is the primary key, may also be in your base model
         lineitems []lineitem `gorm:"foreignkey:parentid"`
         ..other fields
     }
    
     type lineitem struct {
         id          //primary key of lineitem
         parentid   //this is the custom column referencing invoice.id
         ..other fields
     }
  3. 或兩個結構體有不同的欄位名稱

    type Invoice struct {
         ID     //this is the primary key, may also be in your base model
         InvoiceNum 
         Lineitems []LineItem `gorm:"foreignKey:ParentNum;references:InvoiceNum"`
         ..other fields
     }
    
     type LineItem struct {
         ID          //primary key of LineItem
         ParentNum   //this is now referencing Invoice.InvoiceNum
         ..other fields
     }

以上是在 Gorm 中實現與值數組的關係的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:stackoverflow。如有侵權,請聯絡admin@php.cn刪除
Golang vs. Python:並發和多線程Golang vs. Python:並發和多線程Apr 17, 2025 am 12:20 AM

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

Golang和C:性能的權衡Golang和C:性能的權衡Apr 17, 2025 am 12:18 AM

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

Golang vs. Python:申請和用例Golang vs. Python:申請和用例Apr 17, 2025 am 12:17 AM

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

Golang vs. Python:主要差異和相似之處Golang vs. Python:主要差異和相似之處Apr 17, 2025 am 12:15 AM

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

Golang vs. Python:易於使用和學習曲線Golang vs. Python:易於使用和學習曲線Apr 17, 2025 am 12:12 AM

Golang和Python分別在哪些方面更易用和學習曲線更平緩? Golang更適合高並發和高性能需求,學習曲線對有C語言背景的開發者較平緩。 Python更適合數據科學和快速原型設計,學習曲線對初學者非常平緩。

表演競賽:Golang vs.C表演競賽:Golang vs.CApr 16, 2025 am 12:07 AM

Golang和C 在性能競賽中的表現各有優勢:1)Golang適合高並發和快速開發,2)C 提供更高性能和細粒度控制。選擇應基於項目需求和團隊技術棧。

Golang vs.C:代碼示例和績效分析Golang vs.C:代碼示例和績效分析Apr 15, 2025 am 12:03 AM

Golang適合快速開發和並發編程,而C 更適合需要極致性能和底層控制的項目。 1)Golang的並發模型通過goroutine和channel簡化並發編程。 2)C 的模板編程提供泛型代碼和性能優化。 3)Golang的垃圾回收方便但可能影響性能,C 的內存管理複雜但控制精細。

Golang的影響:速度,效率和簡單性Golang的影響:速度,效率和簡單性Apr 14, 2025 am 12:11 AM

goimpactsdevelopmentpositationality throughspeed,效率和模擬性。 1)速度:gocompilesquicklyandrunseff,IdealforlargeProjects.2)效率:效率:ITScomprehenSevestAndardArdardArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增強的Depleflovelmentimency.3)簡單性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具