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

How to define []string in Gorm Model #4498

Closed
Urento opened this issue Jul 5, 2021 · 7 comments
Closed

How to define []string in Gorm Model #4498

Urento opened this issue Jul 5, 2021 · 7 comments
Assignees
Labels
type:question general questions

Comments

@Urento
Copy link

Urento commented Jul 5, 2021

I have been trying to define a []string in a Gorm Model looking like this

type Shoppinglist struct {
	Model

	Title        string
	Items        []string
	Owner        string
	Participants []string
}

My Model struct looks like this:

type Model struct {
	ID         int `gorm:"private_key" json:"id"`
	CreatedOn  int `gorm:"autoCreateTime" json:"created_on"`
	ModifiedOn int `gorm:"autoUpdateTime:milli" json:"modified_on"`
}

When I try to AutoMigrate it like this:
db.AutoMigrate(&Shoppinglist{})

I get an error:

unsupported data type: &[]

And I think its the []string because I can't find it in an example in the documentation. I would highly appreciate any help!

The document you expected this should be explained

I'd expect it to be explained in https://gorm.io/docs/models.html

Expected answer

I want to know if something is wrong with my Model and if, what?
E.g. is it the []string or is it something completly different. And if it is the []string how do I store an Array?

@Urento Urento added the type:question general questions label Jul 5, 2021
@Urento
Copy link
Author

Urento commented Jul 5, 2021

I also got this error

failed to parse value &models.Shoppinglist{Model:models.Model{ID:0, CreatedOn:0, ModifiedOn:0}, Title:"", Items:[]string(nil), Owner:"", Participants:[]string(nil)}, got error unsupported data type: &[]

@Urento
Copy link
Author

Urento commented Jul 5, 2021

This is the test i wrote (I am very new to Go btw):

func TestCreate(t *testing.T) {
	Setup()

	id := rand.Intn(10000)
	title := StringWithCharset(20)
	items := []string{StringWithCharset(45), StringWithCharset(45), StringWithCharset(45), StringWithCharset(45)}
	owner := StringWithCharset(30)
	participants := []string{StringWithCharset(45), StringWithCharset(45), StringWithCharset(45), StringWithCharset(45)}
	shoppinglist := Shoppinglist{
		ID:           id,
		Title:        title,
		Items:        items,
		Owner:        owner,
		Participants: participants,
	}
	err := shoppinglist.Create()
	if err != nil {
		fmt.Println(err.Error())
		t.Errorf("Failed to create shoppinglist")
	}

	list := Shoppinglist{ID: id}
	exists, err := list.ExistsByID()
	if err != nil || !exists {
		t.Errorf("Shoppinglist did not get created")
	}

	l, err := list.GetList()
	if err != nil {
		t.Errorf("Shoppinglist not found")
	}

	if l.ID != id || !testEq(l.Items, items) || l.Owner != owner || !testEq(l.Participants, participants) || l.Title != title {
		t.Errorf("Values weren't correct ")
	}

	err = list.Delete()
	if err != nil {
		t.Errorf("Unable to delete the list")
	}

}

The Error gets thrown from by the Create Method

func (s *Shoppinglist) Create() error {
	shoppinglist := map[string]interface{}{
		"id":           s.ID,
		"title":        s.Title,
		"items":        s.Items,
		"owner":        s.Owner,
		"participants": s.Participants,
	}

	if err := models.CreateList(shoppinglist); err != nil {
		log.Fatal(err.Error())
		return err
	}

	return nil
}
func CreateList(data map[string]interface{}) error {
	list := Shoppinglist{
		Title:        data["title"].(string),
		Items:        data["items"].([]string),
		Owner:        data["owner"].(string),
		Participants: data["participants"].([]string),
	}
	if err := db.Create(&list).Error; err != nil {
		return err
	}
	return nil
}

@Urento Urento closed this as completed Jul 6, 2021
@Urento
Copy link
Author

Urento commented Jul 6, 2021

Got it to work when I changed the string[] to pq.StringArray gorm:"type:text[]"

@Urento Urento reopened this Jul 6, 2021
@Urento Urento closed this as completed Jul 6, 2021
@mike-lloyd03
Copy link

Got it to work when I changed the string[] to pq.StringArray gorm:"type:text[]"

Is this documented anywhere? Spent a lot of time trying to find this answer.

@zbysir
Copy link

zbysir commented Feb 27, 2023

Got it to work when I changed the string[] to pq.StringArray gorm:"type:text[]"

Is this documented anywhere? Spent a lot of time trying to find this answer.

Me too, the docs are a bit rotten. It would be better if the error message was accompanied by suggestions, such as 'maybe you need to add type definition to the tag'

@jinzhu
Copy link
Member

jinzhu commented Feb 27, 2023

Got it to work when I changed the string[] to pq.StringArray gorm:"type:text[]"

Is this documented anywhere? Spent a lot of time trying to find this answer.

https://gorm.io/docs/serializer.html

@zbysir
Copy link

zbysir commented Feb 27, 2023

Got it to work when I changed the string[] to pq.StringArray gorm:"type:text[]"

Is this documented anywhere? Spent a lot of time trying to find this answer.

https://gorm.io/docs/serializer.html

Hi~ @jinzhu, Guess which one of them works?

  1. pq.StringArray
type Post struct {
  Title  string
  Tags   pq.StringArray
}
  1. pq.StringArray and type
type Post struct {
  Title  string
  Tags   pq.StringArray `gorm:"type:text[]"`
}
  1. []string and type
type Post struct {
  Title  string
  Tags   []string `gorm:"type:text[]"`
}
  1. []string and serializer
type Post struct {
  Title  string
  Tags   []string `gorm:"serializer:json"`
}
  1. []string and serializer and type
type Post struct {
  Title  string
  Tags   []string `gorm:"type:text[]; serializer:json"`
}

If they both work, which one is better to use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

4 participants