Hello,
I'm trying to scan the result of a query into a result structure that's comprised of gorm models.
The code builds and the query passes but the result array consists of default values like this:
{{0 0 0 0 0 0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0 0001-01-01 00:00:00 +0000 UTC { false}} {0 0 0 0 {0 false} {0 false} {0 false} 0001-01-01 00:00:00 +0000 UTC false {0 false} {0 false} { false} { false}}}
Also, result array has the exact length as the query result should have (when i try it manually through pgadmin) but they are not mapped correctly.
Is this possible or it's a bug.
Code:
package main
import (
"fmt"
"test/model"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
type Result struct {
model1 model.model1
model2 model.model2
}
func main() {
var result []Result
var err error
db, err := gorm.Open("postgres", "bla")
defer db.Close()
err = db.Raw(`SELECT t1.*, t2.*
FROM t1
INNER JOIN t2 on something
WHERE something`).Scan(&result).Error
for _, element := range result {
fmt.Println(element)
}
}
I have the same problem, did you found any solution ?
The only solution i've come up with goes something like this:
for rows.Next() {
var tempModel1 Model1
var tempModel2 Model2
rows.Scan(&tempModel1.ID, &tempModel1.someProperty, &tempModel1.someOtherProperty, &tempModel2.ID, &tempModel2.someProperty, &tempModel2.someOtherProperty)
//Do something with the temporary objects e.g. append them to the "main" array
}
The problem are the Italic letters, try this:
type Result struct {
Model1 model.model1
Model2 model.model2
}
This does not seem to work for me either. In my case, Result would be a struct that is not an actual model in the db. For example,
type Result struct {
field1 int
field2 string
Model ModelInDB
}
Fields 1 and 2 are attained through a join with the the ModelInDB table. I get both of these fields but I do not get Model. Any ideas?
didn't solve my problem either. I had to use this:
https://github.com/jinzhu/gorm/issues/1815
could be great if this functionality had been better supported
@ruggertech - link does not exist...
are you able to share your solution?
Most helpful comment
@ruggertech - link does not exist...
are you able to share your solution?