ColumnExpr doesn't work in Relation function
irmarini opened this issue · 0 comments
Issue tracker is used for reporting bugs and discussing new features. Please use
Discord or stackoverflow for supporting
issues.
Hi, I'm using go-pg to add prefix to a value of a column in a relation but I always get error column not found. My model:
type BankAccount struct {
tableName struct{} `pg:"bank_account"`
ID int `json:"id" pg:"id,pk"`
Name string `json:"name" pg:"name"`
Number string `json:"number" pg:"number"`
BankCode string `json:"bank_code" pg:"bank_code"`
Bank *Bank `json:"bank,omitempty" pg:"rel:has-one"`
}
type Bank struct {
tableName struct{} `pg:"bank"`
Code string `json:"code" pg:"code,pk"`
Name string `json:"name" pg:"name"`
Image string `json:"image" pg:"image"`
}
I want to add prefix into column image in table bank like this:
var banks []*BankAccount
err := db.Model(&banks).
Relation("Bank", func(q *orm.Query) (*orm.Query, error) {
return q.Column("bank.*").
ColumnExpr("?||image as image", viper.GetString("base_url")), nil
}).Select()
...
but it always return me error like this:
"error": "pg: can't find column=code in model=BankAccount (prefix the column with underscore or use discard_unknown_columns)"
Expected Behavior
The return under the Relation must be the bank table with prefix on column bank.image
So I expect to get output like this:
{
"id": 1,
"name": "John Doe",
"number": "123",
"bank_code": "bank",
"bank": {
"code": "bank",
"name": "Bank Name",
"image": "http://localhost/bank.png"
}
Current Behavior
The return in Relation function always referring to bank_account not bank and give me this error:
"error": "pg: can't find column=code in model=BankAccount (prefix the column with underscore or use discard_unknown_columns)"
Context (Environment)
I'm using v10