亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關系數據庫導致循環

關系數據庫導致循環

Go
守著星空守著你 2022-11-28 17:00:28
我有以下層次結構,用戶 -> 地圖 -> 元素 -> 帖子 一個用戶可以有一堆地圖,每個地圖都有一些元素,每個元素都有一些帖子。輸入用戶結構{UserID        uint   `gorm:"primarykey;autoIncrement;not_null"`UserName string `json: user_name`FirstName string `json: first_name`LastName  string `json: last_name`Email     string `json:email`Password  []byte `json:"-"`Phone     string `json:"phone"`Maps []Map `gorm:"-"`}類型地圖結構{MapID   uint      `gorm:"primarykey;autoIncrement;not_null"`UserID   uint    `json:userid`User     User      `json:"user"; gorm:"foreignkey:UserID`Title    string    `json:title`Desc     string    `json: "desc"`Elements []Element `gorm:"foreignKey:MapID"`Date     time.Time `json: date`}類型元素結構{ElementID   uint       `gorm:"primarykey;autoIncrement;not_null"`ElementName string     `json: element_name`Desc        string     `json: desc`MapID uint `json:mapid`Map   Map   `json:"map"; gorm:"foreignkey:MapID`Posts       []Post     `gorm:"foreignKey:ElementID"`Date        time.Time  `json: date`UserID uint `json:userid`User   User   `json:"user"; gorm:"foreignkey:UserID`}輸入 Post 結構 {PostId    uint      `gorm:"primarykey;autoIncrement;not_null"`Title     string    `json: p_title`Subject   string    `json: subject`Date      time.Time `json: date`Entry     string    `json: entry_text`ElementID uint `json:elementid`Element   Element   `json:"element"; gorm:"foreignkey:ElementID`UserID uint `json:userid`User   User   `json:"user"; gorm:"foreignkey:UserID`}這一切似乎工作正常,但現在當我從后端發送 JSON 響應時,似乎有可能出現無限循環。當我檢索所有用戶的地圖時,它會列出與創建地圖的用戶相關的用戶對象,但地圖隨后還包含一個元素列表,并且在元素對象中它將列出它所屬的地圖和該地圖對象將再次列出它的所有元素。那么我應該通過在一個方向上預加載層次結構來處理這個問題嗎?var getmaps []models.Map database.DB.Preload("User").Preload("Map").Preload("Elements").Offset(偏移).Limit(限制).Find(&getmaps)或者我應該修復 struct 和 gorm 設置以僅在一個方向上獲得關系?因為返回一個地圖將返回它的元素,每個元素將返回它所屬的地圖,循環回到它的元素等。這個循環也會發生在元素和帖子中,其中一個元素將有很多帖子,這些帖子對象將顯示其元素,而該元素對象將顯示其帖子。我確信有一種理想或最佳的方式來實現這一點,所以只是好奇人們會推薦什么。
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

在您的Post,Map和Element結構中,您具有以下字段:


UserID uint   `json:userid`

User   User   `json:"user"; gorm:"foreignkey:UserID`

您應該User從內容結構中刪除該字段,因為您已經有一個UserID. 在這種情況下,“引用”(ID) 比包括整個用戶對象更明智。如果需要,客戶端可以調用/users/{id}端點并查找更多信息。


還User通過刪除限制結構的內容Maps []Map(負責您提到的循環)。然后您需要設置端點,/user/{id}/maps以便客戶端可以獲取用戶的內容。


這同樣適用于Post和Element。您可以全力以赴只存儲 ID,也可以只存儲一組“子”模型。(地圖嵌入元素,元素不嵌入地圖)。因此,要查找元素的關聯映射,您可以調用 endpoint /maps/{your element's map ID}。Element > Post 相同


type Map struct {

    gorm.Model // this takes care of the ID field

    UserID   uint    `json:userid`

    Title    string    `json:title`

    Desc     string    `json: "desc"`

    Elements []Element // gorm will handle the relationship automatically

    Date     time.Time `json: date`

}

type Element struct {

gorm.Model // includes ID

ElementName string     `json: element_name`

Desc        string     `json: desc`

MapID uint `json:mapid`

// Map   Map  ... This relationship is described by another endpoint - /elements/{elementId}/map to get the related map


Posts       []Post     // gorm handles this

Date        time.Time  `json: date`

UserID uint `json:userid`

}

type Post struct {

    gorm.Model

    Title     string    `json: p_title`

    Subject   string    `json: subject`

    Date      time.Time `json: date`

    Entry     string    `json: entry_text`

    ElementID uint `json:elementid` // gorm will use this as fk

    UserID uint `json:userid`

}

為避免循環,您需要在結構級別使關系成為單向的,并設置更多的 http 路由以朝另一個方向發展(請參閱注釋代碼)。


我描述的是一個簡單的 REST api。微軟有一個很好的概述:https ://learn.microsoft.com/en-us/azure/architecture/best-practices/api-design#organize-the-api-design-around-resources - 特別是客戶/訂單關系你會感興趣的。


在 gorm 方面,您將使用一對多關聯:https ://gorm.io/docs/has_many.html


查看完整回答
反對 回復 2022-11-28
  • 1 回答
  • 0 關注
  • 89 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號