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.
Do we need to use `present?`? Why we are getting empty strings as name?