From ada61eb3730e8a16453dc5144679d9d79a8ba30d Mon Sep 17 00:00:00 2001 From: Karl Haas Date: Sat, 2 Oct 2021 16:35:05 +0200 Subject: [PATCH] fix: preserve eager information when validating models (#664) --- executors.go | 6 ++++++ pop_test.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/executors.go b/executors.go index c382f260..e441f997 100644 --- a/executors.go +++ b/executors.go @@ -97,6 +97,10 @@ func (c *Connection) Save(model interface{}, excludeColumns ...string) error { // If model is a slice, each item of the slice is validated then created in the database. func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...string) (*validate.Errors, error) { sm := NewModel(model, c.Context()) + + isEager := c.eager + hasEagerFields := c.eagerFields + if err := sm.beforeValidate(c); err != nil { return nil, err } @@ -155,6 +159,8 @@ func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...stri } } + c.eager = isEager + c.eagerFields = hasEagerFields return verrs, c.Create(model, excludeColumns...) } diff --git a/pop_test.go b/pop_test.go index 107ec86d..8aa74178 100644 --- a/pop_test.go +++ b/pop_test.go @@ -155,6 +155,10 @@ type Taxis []Taxi // Validate gets run every time you call a "Validate*" (ValidateAndSave, ValidateAndCreate, ValidateAndUpdate) method. // This method is not required and may be deleted. func (b *Book) Validate(tx *Connection) (*validate.Errors, error) { + // Execute another query to test if Validate causes eager creation to fail + if err := tx.All(&Taxis{}); err != nil { + return nil, err + } return validate.Validate( &validators.StringIsPresent{Field: b.Description, Name: "Description"}, ), nil