prepare_query need to be called AFTER update_attributes to ensure invariants