Golang Bun query join with 3 levels

Viewed 19

I am currently using bun from uptrace for accessing database. The ORM relation works really well for now. But I have difficulty when I want to join with 3 level of join. So I have this struct, generally speaking the bulletin has many channels:

type Channel struct {
    ID int `json:"id" bun:",pk,autoincrement"`
    Value string `json:"value"`
}

type Bulletin struct {
    ID int `json:"id" bun:",pk,autoincrement"`
    Body string `json:"body"`
}

type BulletinChannel struct {
    ID int `json:"id" bun:",pk,autoincrement"`
    BulletinID int `json:"-"`
    ChannelID int `json:"-"`
}

I was able to make it channel join inside the BulletinChannel like below, but this not what I wanted.

type Bulletin struct {
    ID int `json:"id" bun:",pk,autoincrement"`
    Body string `json:"body"`
    BulletinChannels []BulletinChannel `json:"channel" bun:"rel:has-many,join:id=bulletin_id"`
}

type BulletinChannel struct {
    ID int `json:"id" bun:",pk,autoincrement"`
    BulletinID int `json:"-"`
    ChannelID int `json:"-"`
    Channel *Channel `json:"channel" bun:"belongs-to,join:id=user_i"`
}

But what I wanted was to have the Channel instance inside the Bulletin like below:

type Bulletin struct {
    ID int `json:"id" bun:",pk,autoincrement"`
    Body string `json:"body"`
    Channels []Channel `json:"channel" bun:"rel:has-many,????"` //what to add here????
}

Is that possible to do this?

0 Answers
Related