Assocations and Relationships

Has Many

type Person struct {
  ID        uuid.UUID `json:"id" db:"id"`
  CreatedAt time.Time `json:"created_at" db:"created_at"`
  UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
  Name      string    `json:"name" db:"name"`
}
create_table("people", func(t) {
  t.Column("id", "uuid", {"primary": true})
  t.Column("name", "string", {})
})
type Pet struct {
  ID        uuid.UUID `json:"id" db:"id"`
  CreatedAt time.Time `json:"created_at" db:"created_at"`
  UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
  Name      string    `json:"name" db:"name"`
  PersonID  uuid.UUID `json:"person_id" db:"person_id"`
}
create_table("pets", func(t) {
  t.Column("id", "uuid", {"primary": true})
  t.Column("name", "string", {})
  t.Column("person_id", "uuid", {})
})
models.DB.Transaction(func(tx *pop.Connection) error {
  person := &models.Person{Name: "Mark"}
  err := tx.Create(person)
  if err != nil {
    return errors.WithStack(err)
  }
  pet := &models.Pet{Name: "Ringo", PersonID: person.ID}
  return tx.Create(pet)
})
models.DB.Transaction(func(tx *pop.Connection) error {
  person := &models.Person{}
  err := tx.First(person)
  if err != nil {
    return errors.WithStack(err)
  }
  pets := &models.Pets{}
  err = tx.BelongsTo(person).All(pets)
  if err != nil {
    return errors.WithStack(err)
  }
  fmt.Println(pets)
  return nil
})
SELECT people.created_at, people.id, people.name, people.updated_at FROM people AS people LIMIT 1
SELECT pets.created_at, pets.id, pets.name, pets.person_id, pets.updated_at FROM pets AS pets WHERE person_id = $1

Has Many Through

type Person struct {
  ID        uuid.UUID `json:"id" db:"id"`
  CreatedAt time.Time `json:"created_at" db:"created_at"`
  UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
  Name      string    `json:"name" db:"name"`
}
create_table("people", func(t) {
  t.Column("id", "uuid", {"primary": true})
  t.Column("name", "string", {})
})
type Pet struct {
  ID        uuid.UUID `json:"id" db:"id"`
  CreatedAt time.Time `json:"created_at" db:"created_at"`
  UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
  Name      string    `json:"name" db:"name"`
}
create_table("pets", func(t) {
  t.Column("id", "uuid", {"primary": true})
  t.Column("name", "string", {})
})
type PetOwner struct {
  ID        uuid.UUID `json:"id" db:"id"`
  CreatedAt time.Time `json:"created_at" db:"created_at"`
  UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
  PersonID  uuid.UUID `json:"person_id" db:"person_id"`
  PetID     uuid.UUID `json:"pet_id" db:"pet_id"`
}
create_table("pet_owners", func(t) {
  t.Column("id", "uuid", {"primary": true})
  t.Column("person_id", "uuid", {})
  t.Column("pet_id", "uuid", {})
})
models.DB.Transaction(func(tx *pop.Connection) error {
  person := &models.Person{Name: "Mark"}
  err := tx.Create(person)
  if err != nil {
    return errors.WithStack(err)
  }
  pet := &models.Pet{Name: "Ringo"}
  err = tx.Create(pet)
  if err != nil {
    return errors.WithStack(err)
  }
  owner := &models.PetOwner{PersonID: person.ID, PetID: pet.ID}
  return tx.Create(owner)
})
models.DB.Transaction(func(tx *pop.Connection) error {
  person := &models.Person{}
  err := tx.First(person)
  if err != nil {
    return errors.WithStack(err)
  }
  pets := &models.Pets{}
  err = tx.BelongsToThrough(person, "pet_owners").All(pets)
  if err != nil {
    return errors.WithStack(err)
  }
  fmt.Println(pets)
  return nil
})
SELECT people.created_at, people.id, people.name, people.updated_at FROM people AS people LIMIT 1
SELECT pets.created_at, pets.id, pets.name, pets.updated_at FROM pets AS pets, pet_owners AS pet_owners WHERE pet_owners.person_id = $1 AND pets.id = pet_owners.pet_id