Pop v4.5.0 Released!

Mark Bates
Buffalo — Rapid Web Development in Go
2 min readApr 16, 2018

--

With the release of v4.5.0, and the merging of this PR, Pop allows you to create models and their associations in one step. You no longer need to create every association separately anymore. Pop will even create join table records for many_to_many associations.

Assuming the following piece of psuedo-code:

user := User{
Name: "Mark Bates",
Books: Books{{Title: "Pop Book", Description: "Pop Book", Isbn: "PB1"}},
FavoriteSong: Song{Title: "Don't know the title"},
Houses: Addresses{
Address{HouseNumber: 1, Street: "Golang"},
},
}
err := tx.Eager().Create(&user)
  1. It will notice Books is a has_many association and it will realize that to actually store every book it will need to get the User ID first. So, it proceeds to store first User data so it can retrieve an ID and then use that ID to fill UserID field in every Book in Books. Later it stores all books in database.
  2. FavoriteSong is a has_one association and it uses same logic described in has_many association. Since User data was previously saved before creating all books, it already knows that User got an ID so it fills its UserID field with that value and FavoriteSong is then stored in database.
  3. Houses for this example is a many_to_many relationship and it will have to deal with two tables in this case: users and addresses. It will need to store all addresses first in the addresses table before saving them in the many to many table. Because User was already stored, it should already have an ID.

For a belongs_to association like shown in the example below, it will need first to create User to retrieve ID value and then fill its UserID field before be saved in database.

book := Book{
Title: "Pop Book",
Description: "Pop Book",
Isbn: "PB1",
User: User{
Name: nulls.NewString("Larry"),
},
}
err := tx.Eager().Create(&book)

A big thank you to Larry M Jordan who has been driving this, and other assocation PRs. Thank you Larry!

Are you a company looking for Buffalo or Go training? The Gopher Guides want to help. With customizable onsite and virtual training, the Guides will bring your team up to speed quickly, and with the knowledge they need to write great code on a daily basis.

--

--