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: unable to alter column type for a column that has (but actually doesn't have) a constraint #71089

Closed
Marcuzz opened this issue Oct 4, 2021 · 2 comments · Fixed by #71097
Assignees
Labels
A-schema-changes C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-blathers-triaged blathers was able to find an owner

Comments

@Marcuzz
Copy link

Marcuzz commented Oct 4, 2021

Describe the problem

When writing some migrations to swap out unique_rowid() with uuids we ran into a peculiar problem where the following error would be thrown, even though there were absolutely no constraints on the id column:

[2021-10-04 20:35:26] [0A000] ERROR: unimplemented: ALTER COLUMN TYPE for a column that has a constraint is currently not supported
[2021-10-04 20:35:26] Hint: You have attempted to use a feature that is not yet implemented.
[2021-10-04 20:35:26] See: https://go.crdb.dev/issue-v/48288/v21.1

This error points to #48288, but we don't actually have a constraint pointing to, or on the column in question.

To Reproduce

In a fresh database you have to run the following queries for the 'bug' to occur. This is a minimum working reproduction of the bug:

SET enable_experimental_alter_column_type_general = true;
CREATE TABLE a
(
    id uuid primary key
);
CREATE TABLE b
(
    id   int8                   primary key,
    a_id uuid references a (id) not null,
    legacy_id int8 not null
);
BEGIN;
ALTER TABLE b
    DROP CONSTRAINT "primary";
ALTER TABLE b
    ADD CONSTRAINT "primary" PRIMARY KEY (legacy_id);
COMMIT;
ALTER TABLE b
    ALTER COLUMN id TYPE float8;

It will throw the aforementioned error, citing that there are constraints on the primary key column in table b, thus it's not able to drop the primary key constraint. There's no extra constraint on the primary key in table b, and we can confirm that with the following query:

SHOW CONSTRAINTS FROM b;

Which outputs:

image

To confirm our theory we can run the following queries (after deleting the previously created b and a tables), which will work without a hitch:

SET enable_experimental_alter_column_type_general = true;
CREATE TABLE a
(
    id uuid primary key
);
CREATE TABLE b
(
    id   int8                   primary key,
    a_id uuid references a (id) not null,
    legacy_id int8 not null
);
BEGIN;
ALTER TABLE b
    DROP CONSTRAINT "primary";
ALTER TABLE b
    ADD CONSTRAINT "primary" PRIMARY KEY (legacy_id);
COMMIT;
-- This makes it work
ALTER TABLE b DROP CONSTRAINT "fk_a_id_ref_a";
ALTER TABLE b
    ALTER COLUMN id TYPE float8;

Notice that we drop the foreign key constraint in this set of queries, and now it succeeds.

Expected behavior

The expected behaviour would be for CockroachDB to not complain that there's a constraint on table b's id column, as there is in fact no constraint on b.id.

Environment:

  • CockroachDB version: 21.1.9
  • Server OS: Cockroach in Docker (cockroachdb/cockroach:v21.1.9)
  • Client app: cockroach sql (and also JetBrains DataGrip)
@Marcuzz Marcuzz added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Oct 4, 2021
@blathers-crl
Copy link

blathers-crl bot commented Oct 4, 2021

Hello, I am Blathers. I am here to help you get the issue triaged.

Hoot - a bug! Though bugs are the bane of my existence, rest assured the wretched thing will get the best of care here.

I have CC'd a few people who may be able to assist you:

If we have not gotten back to your issue within a few business days, you can try the following:

  • Join our community slack channel and ask on #cockroachdb.
  • Try find someone from here if you know they worked closely on the area and CC them.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Oct 4, 2021
@postamar
Copy link
Contributor

postamar commented Oct 4, 2021

Thanks for reporting this, this is rather intriguing. I'll look into this.

@postamar postamar self-assigned this Oct 4, 2021
@blathers-crl blathers-crl bot added the T-sql-schema-deprecated Use T-sql-foundations instead label Oct 4, 2021
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 4, 2021
This commit fixes an implementation bug when checking that the table's
foreign keys didn't hold a reference to the altered column.

Fixes cockroachdb#71089.

Release note (bug fix): fixes a bug which caused ALTER COLUMN TYPE
statements to fail when they shouldn't have.
craig bot pushed a commit that referenced this issue Oct 5, 2021
70840: jobs: fix a flakey test by adopting SucceedsSoon r=postamar a=ajwerner

The test failed because the operation stopped retrying. The defaults
for the retrier were less than the duration of a KV lease.

Fixes #70664.

Release note: None

70944: roachprod: upgrade Azure Ubuntu image to 20.04 r=jlinder a=rail

Previously, currently used Ubuntu 18.04 doesn't support `systemd-run
--same-dir`, which is used by some roachprod scripts. Additionally, GCE
and AWS already use Ubuntu 20.04 based images for roachprod.

Updating the base image to Ubuntu 20.04 fixes the issue above and aligns
the version with other cloud providers.

Release note: None

71097: sql: fix FK check bug in ALTER COLUMN TYPE r=postamar a=postamar

This commit fixes an implementation bug when checking that the table's
foreign keys didn't hold a reference to the altered column.

Fixes #71089.

Release note (bug fix): fixes a bug which caused ALTER COLUMN TYPE
statements to fail when they shouldn't have.

Co-authored-by: Andrew Werner <[email protected]>
Co-authored-by: Rail Aliiev <[email protected]>
Co-authored-by: Marius Posta <[email protected]>
@craig craig bot closed this as completed in 9f6cc9c Oct 5, 2021
blathers-crl bot pushed a commit that referenced this issue Oct 5, 2021
This commit fixes an implementation bug when checking that the table's
foreign keys didn't hold a reference to the altered column.

Fixes #71089.

Release note (bug fix): fixes a bug which caused ALTER COLUMN TYPE
statements to fail when they shouldn't have.
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 5, 2021
This commit fixes an implementation bug when checking that the table's
foreign keys didn't hold a reference to the altered column.

Fixes cockroachdb#71089.

Release note (bug fix): fixes a bug which caused ALTER COLUMN TYPE
statements to fail when they shouldn't have.
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 14, 2021
This commit fixes an implementation bug when checking that the table's
foreign keys didn't hold a reference to the altered column.

Fixes cockroachdb#71089.

Release note (bug fix): fixes a bug which caused ALTER COLUMN TYPE
statements to fail when they shouldn't have.
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 26, 2021
This commit fixes an implementation bug when checking that the table's
foreign keys didn't hold a reference to the altered column.

Fixes cockroachdb#71089.

Release justification: simple fix for blatant correctness bug

Release note (bug fix): fixes a bug which caused ALTER COLUMN TYPE
statements to fail when they shouldn't have.
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 26, 2021
This commit fixes an implementation bug when checking that the table's
foreign keys didn't hold a reference to the altered column.

Fixes cockroachdb#71089.

Release justification: simple fix for blatant correctness bug

Release note (bug fix): fixes a bug which caused ALTER COLUMN TYPE
statements to fail when they shouldn't have.
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 26, 2021
This commit fixes an implementation bug when checking that the table's
foreign keys didn't hold a reference to the altered column.

Fixes cockroachdb#71089.

Release justification: simple fix for blatant correctness bug

Release note (bug fix): fixes a bug which caused ALTER COLUMN TYPE
statements to fail when they shouldn't have.
@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
A-schema-changes C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-blathers-triaged blathers was able to find an owner
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants