CRM-14500: refer to contact id foreign keys dynamically

CRM-14500: refer to contact id foreign keys dynamically