pop icon indicating copy to clipboard operation
pop copied to clipboard

Broken Associations or Broken Docs

Open mikepc opened this issue 6 years ago • 0 comments

Saving "Eager" associations is really one of the core benefits of using an abstraction layer like this.

The documentation for querying structs with associations is OK, but honestly very very little is said about managing the write aspects of this data, so onboarding to pop from the outside, is a real mountain-climb.

I've been debugging this code off and on for hours over the weekend, and to be honest.. it really shouldn't take me this long just to figure this out. There is not enough documentation or the API is just straight-up broken in some way.

Source gets created, but source props are not.

type Source struct {
	ID           uuid.UUID `json:"id" db:"id"`
	Name         string    `json:"name" db:"name"`
	Enabled      bool      `json:"enabled" db:"enabled"`
	PageNumLimit int       `json:"pageNumLimit" db:"page_num_limit"`
	CreatedAt    time.Time `json:"createdAt" db:"created_at"`
	UpdatedAt    time.Time `json:"updatedAt" db:"updated_at"`
	Props        *SourceProps `has_many:"source_prop"`
}
type SourceProp struct {
	ID        uuid.UUID `json:"id" db:"id"`
	Name      string `json:"name" db:"name"`
	Value      string `json:"value" db:"value"`
	CreatedAt time.Time `json:"created_at" db:"created_at"`
	UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
        // Added per: https://blog.gobuffalo.io/associations-with-pop-1-to-1-592f02e2bdd8
	SourceID  uuid.UUID `json:"sourceId" db:"source_id"`
	Source    Source    `belongs_to:"source"`
}
func CreateSource(source *Source, db *storage.Connection) error {
	if err := db.Eager().Create(source); err != nil {
		return err
	}
	return nil
}

	source := models.Source{
		Name:    "my-source-name",
		Enabled: true,
		PageNumLimit: 2,
		Props: &models.SourceProps{
			{Name: "my-key", Value: "my-value"},
			{Name: "another_key", Value: "another_value"},
		},
	}
	models.CreateSource(&source, db)
[[projects]]
  digest = "1:8cfd49043a7320db1153a9e7678b67dd36bb275cb18a939cd34cc216fa057b1d"
  name = "github.com/gobuffalo/pop"
  packages = [
    ".",
    "associations",
    "columns",
    "fix",
    "logging",
    "nulls",
  ]
  pruneopts = "UT"
  revision = "a140925276668f9368d6e57b4ec0ea553c6afdd9"
  version = "v4.9.9"

mikepc avatar Mar 11 '19 17:03 mikepc