diff --git a/pkg/sql/comment_on_constraint.go b/pkg/sql/comment_on_constraint.go index ff498b3e9951..2feea4f12e21 100644 --- a/pkg/sql/comment_on_constraint.go +++ b/pkg/sql/comment_on_constraint.go @@ -17,7 +17,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" @@ -62,8 +61,10 @@ func (n *commentOnConstraintNode) startExec(params runParams) error { if err != nil { return err } - cSchema, ok := schemadesc.GetVirtualSchemaByID(n.tableDesc.GetParentSchemaID()) - if !ok { + schema, err := params.p.Descriptors().GetImmutableSchemaByID( + params.ctx, params.extendedEvalCtx.Txn, n.tableDesc.GetParentSchemaID(), tree.SchemaLookupFlags{}, + ) + if err != nil { return err } @@ -78,16 +79,16 @@ func (n *commentOnConstraintNode) startExec(params runParams) error { switch kind := constraint.Kind; kind { case descpb.ConstraintTypePK: constraintDesc := constraint.Index - n.oid = hasher.PrimaryKeyConstraintOid(n.tableDesc.GetParentID(), cSchema.GetName(), n.tableDesc.GetID(), constraintDesc) + n.oid = hasher.PrimaryKeyConstraintOid(n.tableDesc.GetParentID(), schema.GetName(), n.tableDesc.GetID(), constraintDesc) case descpb.ConstraintTypeFK: constraintDesc := constraint.FK - n.oid = hasher.ForeignKeyConstraintOid(n.tableDesc.GetParentID(), cSchema.GetName(), n.tableDesc.GetID(), constraintDesc) + n.oid = hasher.ForeignKeyConstraintOid(n.tableDesc.GetParentID(), schema.GetName(), n.tableDesc.GetID(), constraintDesc) case descpb.ConstraintTypeUnique: constraintDesc := constraint.Index.ID - n.oid = hasher.UniqueConstraintOid(n.tableDesc.GetParentID(), cSchema.GetName(), n.tableDesc.GetID(), constraintDesc) + n.oid = hasher.UniqueConstraintOid(n.tableDesc.GetParentID(), schema.GetName(), n.tableDesc.GetID(), constraintDesc) case descpb.ConstraintTypeCheck: constraintDesc := constraint.CheckConstraint - n.oid = hasher.CheckConstraintOid(n.tableDesc.GetParentID(), cSchema.GetName(), n.tableDesc.GetID(), constraintDesc) + n.oid = hasher.CheckConstraintOid(n.tableDesc.GetParentID(), schema.GetName(), n.tableDesc.GetID(), constraintDesc) } // Setting the comment to NULL is the diff --git a/pkg/sql/comment_on_constraint_test.go b/pkg/sql/comment_on_constraint_test.go index 9e0265db231c..4ea597c3d18e 100644 --- a/pkg/sql/comment_on_constraint_test.go +++ b/pkg/sql/comment_on_constraint_test.go @@ -33,7 +33,9 @@ func TestCommentOnConstraint(t *testing.T) { CREATE DATABASE d; SET DATABASE = d; CREATE TABLE t ( a int UNIQUE, b numeric CONSTRAINT positive_price CHECK (b > 0), c int CHECK (b > c), CONSTRAINT pkey PRIMARY KEY (a,c)); - CREATE TABLE t2 (a UUID PRIMARY KEY, b int NOT NULL REFERENCES t (a)) + CREATE TABLE t2 (a UUID PRIMARY KEY, b int NOT NULL REFERENCES t (a)); + CREATE SCHEMA s; + CREATE TABLE s.t ( a int UNIQUE, b numeric CONSTRAINT positive_price CHECK (b > 0), c int CHECK (b > c), CONSTRAINT pkey PRIMARY KEY (a,c)); `); err != nil { t.Fatal(err) } @@ -48,6 +50,11 @@ func TestCommentOnConstraint(t *testing.T) { `SELECT obj_description(oid, 'pg_constraint') FROM pg_constraint WHERE conname='t_a_key'`, gosql.NullString{String: `unique_comment`, Valid: true}, }, + { + `COMMENT ON CONSTRAINT t_a_key ON s.t IS 'unique_comment'`, + `SELECT obj_description(oid, 'pg_constraint') FROM pg_constraint WHERE conname='t_a_key'`, + gosql.NullString{String: `unique_comment`, Valid: true}, + }, { `COMMENT ON CONSTRAINT positive_price ON t IS 'check_comment'`, `SELECT obj_description(oid, 'pg_constraint') FROM pg_constraint WHERE conname='positive_price'`,