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

unsupported destination, should be slice or struct #1505

Closed
littletwolee opened this issue Jun 19, 2017 · 3 comments
Closed

unsupported destination, should be slice or struct #1505

littletwolee opened this issue Jun 19, 2017 · 3 comments

Comments

@littletwolee
Copy link

littletwolee commented Jun 19, 2017

What version of Go are you using (go version)?

golang 1.8.3

Which database and its version are you using?

mysql 5.6

What did you do?

Please provide a complete runnable program to reproduce your issue.

package main

import (
	"fmt"

	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
)

var db *gorm.DB

func find(v *map[string]interface{}) {

}

func main() {
	var err error
	db, err = gorm.Open("mysql", "***")
	if err != nil {
		panic(err)
	}
	var user map[string]interface{}
	db.Debug().Table("test").Find(&user)
	fmt.Println(user)
}

Like this demo.I want search some rows from mysql, and I don't want use struct. Beacuse, when I want get a join result, I need to create a new big struct or to payload struct. I think map is good in this.

Any guys can help me ?Thx

@ktsakas
Copy link

ktsakas commented Jun 26, 2017

You have not initialized the user struct.
Try changing it to var user = make(map[string]interface{}).
Although I think only structs and slices are allowed.
I don't think it works with maps.

@littletwolee
Copy link
Author

thx @ktsakas . I was tryed your suggest.But it not work. I think you are right, it is only struct or slices.

@jinzhu jinzhu closed this as completed Feb 12, 2018
@martinskou
Copy link

Maybe this can be of help:

package main

import (
	"fmt"
	"reflect"

	"github.com/jinzhu/gorm"
	_ "github.com/mattn/go-sqlite3"
)

type Project struct {
	gorm.Model
	Name string `gorm:"type:varchar(200)"`
}

func main() {
	db, _ := gorm.Open("sqlite3", "db.sqlite3?cache=shared&mode=rwc")
	db.AutoMigrate(&Project{})

	db.Create(&Project{Name: "A"}) // Demo record inserted

	// Now I would like to use reflection to be able to make a generic function
	t := reflect.TypeOf(Project{})
	fmt.Println(t)

	single := reflect.New(t).Interface()
	db.Debug().Table("projects").First(single)
	fmt.Printf("Single result > %+v\n", single) // Works with a single instance

	multiple := reflect.MakeSlice(reflect.SliceOf(t), 0, 10)
	db.Debug().Table("projects").Find(&multiple)       // Log show table and records are found
	fmt.Printf("1. Multiple result > %+v\n", multiple) // But, multiple is empty

	// Instead:

	// Create a pointer to a slice value and set it to the slice
	// multiple := reflect.MakeSlice(reflect.SliceOf(t), 0, 10)
	multiplepointer := reflect.New(multiple.Type())
	multiplepointer.Elem().Set(multiple)

	db.Debug().Table("projects").Find(multiplepointer.Interface())
	fmt.Printf("2. Multiple result > %+v\n", multiplepointer) // multiplepointer contains rows
}

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

4 participants