Gorm: strings slice causes goroutine to panic

Created on 12 Jan 2014  路  5Comments  路  Source: go-gorm/gorm

hi, just create a simple model that uses []string and it'll crash with something like this https://gist.github.com/AvianFlu/4688697c15b3587623a2

ie:

type User struct {
  Id int64
  Name string
  Emails []string
}

Most helpful comment

@jinzhu, would you be willing to show an example of how to make something like the above example work? I'm working on creating an API, and it would be much more succinct to have something like:

{
    "name" : "Test",
    "products" : [ "product1", "product2" ]
}

As opposed to the current way of doing it:

{
    "name" : "Test",
    "products" : [
          {  "value" : "product1"  },
          {  "value" : "product2"  }
}

Any help would be appreciated!

All 5 comments

hi gorm doesn't support []string because database dialect that you are using don't know how to store it to database.

So maybe you need to write a Scanner for slice.

@jinzhu, would you be willing to show an example of how to make something like the above example work? I'm working on creating an API, and it would be much more succinct to have something like:

{
    "name" : "Test",
    "products" : [ "product1", "product2" ]
}

As opposed to the current way of doing it:

{
    "name" : "Test",
    "products" : [
          {  "value" : "product1"  },
          {  "value" : "product2"  }
}

Any help would be appreciated!

I meet this issue too.

Yea, I'm running into this issue as well, and I have seen a bunch of old issues about this as well.
@jinzhu you've mentioned writing a Scanner or a Valuer for this situation. Do you mind expounding on what you are referring to? Maybe a link to an example.

Ok, I've found the interface definition here: https://golang.org/pkg/database/sql/#Scanner. Hopefully this will do the trick.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ganitzsh picture Ganitzsh  路  3Comments

hypertornado picture hypertornado  路  3Comments

pjebs picture pjebs  路  3Comments

youtwo123 picture youtwo123  路  3Comments

alanyuen picture alanyuen  路  3Comments