1 Commits

Author SHA1 Message Date
Jay Taylor
c53f494ff6 Recalculate fields in `Scope.Fields' when empty (nil) or number of fields is LTE 1.
Protect the `.fields' variable state from partial-initialization timing
issues.

As gorm warms state and makes `.GetStructFields' calls, they lead to a long
deferred function in model_struct.go.  Then the deferred function calls
`Scope.Fields', leading to another `.GetStructFields' call.. cyclically.
Once information is cached the cycling ends.

This extra rule evicts incorrect/suspiciously short fields information.

Symptom of incorrect internal state ocurring can manifest as invalid
insert SQL statement errors generated during `DB.Save', e.g.:

    INSERT INTO "your_table" DEFAULT VALUES RETURNING "your_table"."id"

This is why we refresh if nil or only a single field (usually is the ID field).

Included is a unit-test which triggers the scenario and prove that it is
fixed.
2015-08-07 22:17:51 -07:00