pg
pg copied to clipboard
Can't back reference in many2many relation
type Store struct {
Id uuid.UUID `pg:"type:uuid"`
User *User
UserId uuid.UUID `pg:"type:uuid"`
Name string `pg:",unique"`
Slug string `pg:",unique"`
Invitations []*StoreInvitation
BusinessCategories []*BusinessCategory `pg:"many2many:categories_to_stores"`
Users []*User `pg:"many2many:users_to_stores"`
CreatedAt time.Time
}
type UsersToStore struct {
StoreId uuid.UUID `pg:"type:uuid,pk"`
Store *Store
UserId uuid.UUID `pg:"type:uuid,pk"`
User *User
CreatedAt time.Time
}
type User struct {
Id uuid.UUID `pg:"type:uuid"`
Email string `pg:",unique"`
Username string `pg:",unique"`
ExternalAccounts []*ExternalAccount
Stores []*Store `pg:"many2many:users_to_stores"`
CreatedAt time.Time
}
Get all Users from Store perspective works
h.db.Model(&store).Where("user_id = ?", claims.UserId).Relation("Users")
but getting all stores from user perspective fails
h.db.Model(user).Where("id = ?", userId).Relation("Stores.*").First()
with the error pg: model=Store has no column=\"user_id\""
but the resulting query works! I tried several other combinations, read the examples but I have no idea why it failed.
It's very similar to https://github.com/go-pg/pg/issues/1422 but I don't find out the exact solution.
any idea?