Updated unique key generation to check in both tables.