Updated unique key generation to check the generated key in both tables.