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

Add support for primary key constraints in create_table operation #594

Merged
merged 31 commits into from
Jan 17, 2025

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Jan 14, 2025

This PR adds support for setting primary key constraints in the constraints option of create_table.

Please note that if you set primary keys in columns, you are not allowed to configure primary_key constraints.

This configuration method is superior to marking columns as primary keys in the list columns, because it supports indexing options and deferring constraint checks.

Allowed options:

  • columns (required)
  • index_parameters.tablespace
  • index_parameters.storage_parameters
  • index_parameters.include_columns
  • deferrable
  • initially_deferred

Example:

{
  "name": "50_create_table_with_table_constraint",
  "operations": [
    {
      "create_table": {
        "name": "phonebook",
        "columns": [
          {
            "name": "id",
            "type": "serial",
          },
          {
            "name": "name",
            "type": "varchar(255)"
          }
        ],
        "constraints": [
          {
            "name": "my_pk",
            "type": "primary_key",
            "columns": [
              "id"
            ] 
          }
        ]
      }
    }
  ]
}

@kvch kvch marked this pull request as ready for review January 16, 2025 13:06
@kvch kvch enabled auto-merge (squash) January 16, 2025 13:06
@kvch kvch requested a review from andrew-farries January 16, 2025 13:07
Copy link
Collaborator

@andrew-farries andrew-farries left a comment

Choose a reason for hiding this comment

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

This looks nice 👍

Can we also add an example migration that shows how to create PKs using a table constraint?

pkg/sql2pgroll/create_table_test.go Show resolved Hide resolved
@kvch kvch requested a review from andrew-farries January 17, 2025 11:06
@kvch kvch merged commit 5df19ff into xataio:main Jan 17, 2025
28 checks passed
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.

2 participants