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.
Yes :cry: