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

sql: drop and add constraint with same name in one statement #96831

Open
Xiang-Gu opened this issue Feb 8, 2023 · 1 comment
Open

sql: drop and add constraint with same name in one statement #96831

Xiang-Gu opened this issue Feb 8, 2023 · 1 comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@Xiang-Gu
Copy link
Contributor

Xiang-Gu commented Feb 8, 2023

The following problem exists in legacy schema changer:

Setup:

CREATE TABLE t (i INT PRIMARY KEY);
ALTER TABLE t ADD CONSTRAINT check_i CHECK (i > 0);

Troubled Stmt:

ALTER TABLE t DROP CONSTRAINT check_i, ADD CONSTRAINT check_i CHECK (i > 0), VALIDATE CONSTRAINT check_i;

(The first easy-to-fix error is the ADD CONSTRAINT will complain that the name "check_i" is already used. This is easily fixed by adding some logic in alter_table.go:validateConstraintNameIsNotUsed)

After that, it still errors with the VALIDATE CONSTRAINT clause saying "constraint check_i does not exist". The root cause is that we deduplicated when we build the constraint cache so that catalog.FindConstraintByName in VALIDATE CONSTRAINT will see the dropping constraint check_i, and fail to realize there is another adding constraint (with the same name).

I don't have an idea on how to fix it elegantly, and it's probably not worth it since this can be beautifully handled in the declarative schema changer, but I still open this issue for tracking.

Jira issue: CRDB-24354

@Xiang-Gu Xiang-Gu added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Feb 8, 2023
@blathers-crl blathers-crl bot added the T-sql-schema-deprecated Use T-sql-foundations instead label Feb 8, 2023
@Xiang-Gu
Copy link
Contributor Author

Xiang-Gu commented Feb 8, 2023

#96648 is the PR that allows the declarative schema changer to handle this case correctly.

@exalate-issue-sync exalate-issue-sync bot added T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) and removed T-sql-schema-deprecated Use T-sql-foundations instead labels May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
None yet
Development

No branches or pull requests

1 participant