(Translated by https://www.hiragana.jp/)
ColumnExpr doesn't work in Relation function · Issue #1852 · go-pg/pg · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ColumnExpr doesn't work in Relation function #1852

Open
irmarini opened this issue Mar 23, 2021 · 0 comments
Open

ColumnExpr doesn't work in Relation function #1852

irmarini opened this issue Mar 23, 2021 · 0 comments

Comments

@irmarini
Copy link

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

@irmarini irmarini changed the title Can't add column expression in Relation function ColumnExpr does'nt work in Relation function Mar 23, 2021
@irmarini irmarini changed the title ColumnExpr does'nt work in Relation function ColumnExpr doesn't work in Relation function Mar 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant