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

Support creating foreign key constraints on the create table operation #79

Merged
merged 4 commits into from
Sep 5, 2023

Conversation

andrew-farries
Copy link
Collaborator

Allow creating foreign key columns when doing a create table operation. For example:

{
  "name": "19_create_orders_table",
  "operations": [
    {
      "create_table": {
        "name": "orders",
        "columns": [
          {
            "name": "id",
            "type": "serial",
            "pk": true
          },
          {
            "name": "user_id",
            "type": "integer",
            "references": {
              "table": "users",
              "column": "id"
            }
          },
          {
            "name": "quantity",
            "type": "int"
          }
        ]
      }
    }
  ]
}

Here the user_id column references the id column in the users table.

The constraint is added to the table on Start and removed on Rollback.

@andrew-farries andrew-farries changed the title Support creating foreign key constraints on the **create table** operation Support creating foreign key constraints on the create table operation Sep 4, 2023
Unique bool `json:"unique"`
PrimaryKey bool `json:"pk"`
Default *string `json:"default"`
References *ColumnReference `json:"references"`
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should make this part of the column definition or the table. The reason is that foreign keys can also be a constraint at the table level, and they can reference several columns instead of just one.

I guess we are fine with this for now and we can allow foreign key definition at the table level later? It would definitely be needed to support multi column foreign keys

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've created #81 to track multi-column foreign keys.

@andrew-farries andrew-farries merged commit f94d252 into main Sep 5, 2023
3 checks passed
@andrew-farries andrew-farries deleted the allow-fks-in-create-table branch September 5, 2023 12:25
andrew-farries added a commit that referenced this pull request Sep 6, 2023
#80)

Allow the **add column** operation to create foreign key columns.

An example of such an operation is:

```json
{
  "name": "17_add_rating_column",
  "operations": [
    {
      "add_column": {
        "table": "orders",
        "column": {
          "name": "user_id",
          "type": "integer",
          "references": {
            "table": "users",
            "column": "id",
          }
        }
      }
    }
  ]
}
```

Most of the work to support the operation is in
#79.

* The constraint is added on `Start` (named according to the temporary
name of the new column).
* The entire new column, including the foreign key constraint, is
removed on `Rollback`.
* The constraint is renamed to use the final name of the new column on
`Complete`.

Test cases are included for both nullable and non-nullable FKs.
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.

None yet

2 participants