Multiple same-type belongs-to fields not working (?)
I'm implementing a database architecture which has 1 one-to-many relation and the 'many' tables implement a belongs-to. The architecture looks like this:
type Pair struct {
Id uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
PairRefer uint64 `json:"refer"`
}
type PairCouple struct {
Id uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
PairCoupleRefer uint64 `json:"refer"`
BuyPair Pair `json:"buy_pair" gorm:"foreignKey:PairRefer"` // gorm:"embedded;column:buy_pair"
SellPair Pair `json:"sell_pair" gorm:"foreignKey:PairRefer"`
}
type Coin struct {
Id uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
ArbitragePairs []PairCouple `json:"pair_couples" gorm:"foreignKey:PairCoupleRefer;references:Id"`
}
In my code I retrieve all 'Coin' records and iterate them to access all data, PairCouple one-to-many works just fine, but then when i access the BuyPair/SellPair the fields are EMPTY
I honestly don't think its an issue related to the associations but rather to the preloading, i honestly have no clue at this point...
this is how i retrieve the records:
tx = dbCtx.Statement.DB.Preload("BuyPair").Preload("SellPair").Preload("ArbitragePairs").Find(&coins)
but this invokes an error
'BuyPair' unsupported relations for schema Coin
Comment From: black-06
Try this:
err := db.
Preload("ArbitragePairs.BuyPair").
Preload("ArbitragePairs.SellPair").
Preload("ArbitragePairs").
Find(&coins)
But gorm doesn't known how to preload when foreign keys are the same. May be you should use Hasone relatioin
type Pair struct {
Id uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
Name string
}
type PairCouple struct {
Id uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
PairCoupleRefer uint64 `json:"refer"`
BuyPair Pair `json:"buy_pair"` // gorm:"embedded;column:buy_pair"
BuyPairID uint64
SellPair Pair `json:"sell_pair"`
SellPairID uint64
Name string
}
type Coin struct {
Id uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
ArbitragePairs []PairCouple `json:"pair_couples" gorm:"foreignKey:PairCoupleRefer;references:Id"`
Name string
}
Comment From: Duality1k
Try this:
go err := db. Preload("ArbitragePairs.BuyPair"). Preload("ArbitragePairs.SellPair"). Preload("ArbitragePairs"). Find(&coins)But gorm doesn't known how to preload when foreign keys are the same. May be you should use Hasone relatioin
``go type Pair struct { Id uint64json:"id" gorm:"primaryKey;autoIncrement"` Name string }type PairCouple struct { Id uint64
json:"id" gorm:"primaryKey;autoIncrement"PairCoupleRefer uint64json:"refer"BuyPair Pairjson:"buy_pair"// gorm:"embedded;column:buy_pair" BuyPairID uint64 SellPair Pairjson:"sell_pair"SellPairID uint64 Name string }type Coin struct { Id uint64
json:"id" gorm:"primaryKey;autoIncrement"ArbitragePairs []PairCouplejson:"pair_couples" gorm:"foreignKey:PairCoupleRefer;references:Id"Name string } ```
Hey, thanks for the reply. I've tried using SellPairID and BuyPairID as a FK too, but no luck. Altought your preloading + separate fk's seems to work fine. Thanks alot :)