As of December 1st, 2019 Buffalo, and all related packages, require Go Modules and the use of the GOPATH is no longer supported.

Please see this blog post for more information

One to one associations

In this chapter, you'll learn how to write a one to one association in Pop.


One to one associations work using a pair of tags:

  • belongs_to for the model with the foreign key.
  • has_one for the model without the foreign key.


// Models

type Head struct {
  ID           int
  BodyID       int        `db:"body_id"`
  Body         *Body      `belongs_to:"body"`

type Body struct {
  ID           int
  Head         Head       `has_one:"head"`
// Eager creation:
// Create a body with its head.
b := &models.Body{
    Head: models.Head{},

if err := tx.Eager().Create(b); err != nil {
    return err
// Eager fetch all bodies with their head.
bodies = &models.Bodies{}

if err := c.Eager().All(bodies); err != nil {
    log.Printf("err: %v", err)

log.Printf("eager fetch: %v", bodies)

Related Content