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

bug: Inconsistant Constraints Post migration #17451

Open
bbernays opened this issue Apr 1, 2024 · 0 comments
Open

bug: Inconsistant Constraints Post migration #17451

bbernays opened this issue Apr 1, 2024 · 0 comments

Comments

@bbernays
Copy link
Collaborator

bbernays commented Apr 1, 2024

Postgres has an optimization that stops the creation of duplicate indexes in the case of a Unique constraint being applied to the primary key. So the following two DML statements are not equivalent:

This is how we migrate users to the new primary key:

CREATE TABLE IF NOT EXISTS "aws_table_initial" (
    "_cq_id" uuid UNIQUE NOT NULL,
    "account_id" text,
    CONSTRAINT "aws_table_initial_cqpk" PRIMARY KEY ("account_id")
)
ALTER TABLE aws_table_initial DROP CONSTRAINT aws_table_initial_cqpk
ALTER TABLE aws_table_initial ADD CONSTRAINT aws_table_initial_cqpk PRIMARY KEY ("_cq_id")
  1. Creating a new table with only _cq_id as the PK:
CREATE TABLE IF NOT EXISTS "aws_table_initial_v2" (
    "_cq_id" uuid UNIQUE NOT NULL,
    "account_id" text,
    CONSTRAINT "aws_table_initial_cqpk" PRIMARY KEY ("_cq_id")
)

Statement (1) results in two constraints on cq_id (aws_table_initial_cqpk and aws_table_initial__cq_id_key) while statement (2) results in only a single constraint aws_table_initial_v2_cqpk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

1 participant