Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix read_schema function for multi-column foreign key constaints #476

Merged
merged 2 commits into from
Nov 21, 2024

Conversation

andrew-farries
Copy link
Collaborator

Update the query used by read_schema to fetch foreign key information. This fixes the problem of columns and referencedColumns containing duplicate entries for multi-column foreign keys.

For a foreign key defined like "fk_sellers" FOREIGN KEY (sellers_name, sellers_zip) REFERENCES sellers(name, zip) the change in behaviour is as follows:

Before:

{
      ...
      "foreignKeys": {
        "fk_sellers": {
          "name": "fk_sellers",
          "columns": [
            "sellers_name",
            "sellers_zip",
            "sellers_name",
            "sellers_zip"
          ],
          "referencedTable": "sellers",
          "referencedColumns": [
            "name",
            "zip",
            "name",
            "zip"
          ],
          "onDelete": "NO ACTION"
        }
      },
}

After:

{
      ...
      "foreignKeys": {
        "fk_sellers": {
          "name": "fk_sellers",
          "columns": [
            "sellers_name",
            "sellers_zip"
          ],
          "referencedTable": "sellers",
          "referencedColumns": [
            "name",
            "zip"
          ],
          "onDelete": "NO ACTION"
        }
      },
}

The solution here is to perform grouping and array aggregation of the referencing table's columns before the join to the referenced table's columns.

Fixes #475

@andrew-farries andrew-farries marked this pull request as ready for review November 21, 2024 08:57
Copy link
Member

@exekias exekias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@andrew-farries andrew-farries merged commit 0b08a1d into main Nov 21, 2024
27 checks passed
@andrew-farries andrew-farries deleted the fix-read-schema-for-multi-column-fks branch November 21, 2024 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multi column foreign key constraints are not captured correctly by read_schema
2 participants