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.
This commit adds support for two settings:
FOREIGNTYPE - A field that is used to store the type of the owner.
POLYMORPHIC - A shortcut to set FOREIGNKEY and FOREIGNTYPE to the same
value suffixed by "Id" and "Type" respectively.
The type is stored as the table name, which I thought might be useful
for other queries.
The biggest gotcha of this commit is that I flipped the definition of
has_one and belongs_to. gorm is very flexible such that it didn't
really care if it was a has_one or belongs_to, and can pretty much
determine it at runtime. For the sake of the error, I had to define
one of them as belongs_to, and I chose the one with the fields as
the belongs_to, like ActiveRecord. The error could probably be
genericized to "gorm cannot determine type", but I think it's nicer
to tell people DONT DO PATTERN XYZ CAUSE IT WONT WORK. Functionally,
it doesn't matter.