stephenafamo/bob

Gracefully handle foreign key constraints against the same table

plunkettscott opened this issue · 1 comments

I have added some foreign keys for created_by_id and updated_by_id which both reference the same table. Today, when this happens Bob generates two relationships with the same name causing the code to not compile.

CleanShot 2023-09-28 at 19 00 20

I feel that in the event of duplicates, the column name should be appended or prepended to the relationship key in R struct. I can get around this manually by specifying mappings in the config file, but it's getting to be pretty hard to manage tables where these constraints exist.

This is one of those cases where it is difficult to automatically figure out a name. While column names can be appended, that would require the list of all relationships to be known while trying to pick a name and adds complexity for a relatively rare situation.

In this case, the best way would be to change this using the aliases configuration:

https://bob.stephenafamo.com/docs/code-generation/configuration#aliases

# Although team_names works fine without configuration, we use it here for illustrative purposes
aliases:
  tables:
    org_ip_blocks:
      relationships: 
        org_ip_blocks.org_ip_blocks_created_by_id_fkey: "MemberCreated"
        org_ip_blocks.org_ip_blocks_updated_by_id_fkey: "MemberUpdated"