Move writing unknown column exception to null attribute

Making this change revealed several subtle bugs related to models with
no primary key, and anonymous classes. These have been fixed as well,
with regression tests added.
Merge pull request #15924 from sgrif/sg-write-unknown-column

Move writing unknown column exception to null attribute