moved insert command in upsert, because default values may not be acceptable (e.g. NOT NULL)