go-pg: Push data to existing array

480 views Asked by At

I want to add a new value to a string array in postgres db using go-pg. How could I push a new value to the array using go-pg.

This is my user model

type User struct {
    ID        string    `pg:"id,notnull,unique" json:"id"`
    Email     string    `pg:"email,notnull,unique" json:"email"`
    Skills    []string  `pg:"skills,array" json:"skill"`
}

I want to update the skills array whenever user adds a new skill using the userid.

Data from client contains the userid and a single skill selected by the user.

In go-pg we can update a column in the following way

pg.Model(user).Set("data = ?data").Where("id = ?id").Update()

But how could I push a new data to the array?

1

There are 1 answers

0
Amal Jose On BEST ANSWER

As Yun Luo suggested I have done with array_append.

pg.Exec(`UPDATE users SET skills = array_append(skills, 'skill') WHERE id = 'id';`)