Open frederikhors opened 4 years ago
@vmihailenco I refactored question.
Maybe I'm completely wrong to think about the problem.
But precisely for this there is the open source community and geniuses like you.
Hi @frederikhors,
Perhaps I misunderstand the problem, but when I need virtual column I usually add one to the model, e.g.
type Player struct {
ID int
Name string
// Virtual columns.
Mycalculation string `pg:"-"`
}
Your solution with playerWithCalculation
looks perfectly fine too and in some cases I use it too.
Hope that helps.
I'm studying Golang trying to build an application.
I have written many models:
and so on...
A model is like this:
I'm using go-PG like this:
and it works.
The SQL query is:
with these rows:
Now I need to query from DB one or more "virtual" columns like this:
so I tried to use:
which generates a correct SQL query:
with these rows:
I'm doing this because I am interested in the result in the
mycalculation
column which is much more convenient to calculate in the database than in Go; it can be JSON or string. Simply data.Now my ORM panic with:
PNC error="pg: can't find column=mycalculation in model=Player (try discard_unknown_columns)"
I can understand that because now go-pg doesn't know how to bind data, so now I'm using:
QUESTION
I think this is wasteful and expensive. Many allocations and loops.
And besides, I have several models and resolvers and methods!
What can I do?
How can I improve this code and my architecture?
I think a possible solution is to run two queries, perhaps in two different goroutines. But I don't want to run two queries for this.