Rodrigo
Rodrigo

Reputation: 3278

inserting relationships go-pg PostgreSQL

I've got 2 structs to represent a ManyToMany relationship. User and Note

type User struct {
    ID int
    Name string
    Notes []*Note
}

type Note struct {
    TableName struct{} `sql:"user_notes"`
    ID int
    Text string
}

Now let's say I want to insert a new user and also at the same time add a few notes.

I would expect this to insert a user and its note(s):

note := Note{
    Text: "alohaa dude",
}

user := User{
    Name:  "peter",
    Notes: []Note{no},
}

s.DB.Insert(&user)

However this only saves the user and not the user and the note. In go-pg do I have to do this manually or is there an automated way through the ORM?

Upvotes: 2

Views: 3329

Answers (1)

user3939196
user3939196

Reputation: 11

Rodrigo, same problem statement is being discussed here: https://github.com/go-pg/pg/issues/478

This functionality is not supported in go-pg at this time and you might want to try a db prepare approach to insert with relationships.

Upvotes: 1

Related Questions