Adding logic to ensure that the primary key is set