Reputation: 111335
I have a bunch of similar temp tables which I am trying to query using go-pg's ORM. I can't find a way to dynamically change the queried table during a select:
import "gopkg.in/pg.v4"
type MyModel struct {
TableName struct{} `sql:"temp_table1"`
Id int64
Name string
}
var mymodels []MyModel
err := db.Model(&mymodels).Column("mymodel.id", "mymodel.name").Select()
This will query temp_table1
as defined in the model's TableName
. Is there a way to pass table name as a parameter so I can query temp_table_X
?
(I can just not use ORM and go with raw db.Query()
, but I wanted to see if there is a way to use ORM).
Upvotes: 3
Views: 5435
Reputation: 111335
Got an answer on github:
err := db.Model().TableExpr("temp_table_999 AS mymodel").Column("mymodel.id", "mymodel.name").Select(&mymodels)
Upvotes: 2
Reputation: 1190
Seems you can specify the table directly: db.Model(&mymodels).Table('temp_table1').Column("mymodel.id", "mymodel.name").Select()
Upvotes: 0