cockroachdb / cockroachdb/cockroach

sql: it is not possible to rename a table that doesn't have a primary key

Open
#112,090 2 comments 0 reactions 0 assignees View on GitHub
A-tools-efcore C-bug O-community T-sql-foundations X-blathers-triaged
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Describe the problem**

Given a table as follows

```
CREATE TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
```

The scenario is to rename to table to `Persons` and at the same time rename the primary key constraint to `PK_Persons`. The following SQL is used for the migration

```
BEGIN;
ALTER TABLE "People" DROP CONSTRAINT "PK_People";

ALTER TABLE "People" RENAME TO "Persons";

ALTER TABLE "Persons" ADD CONSTRAINT "PK_Persons" PRIMARY KEY ("Id");
COMMIT;
```

which returns

```
SQLSTATE: 55C02
ERROR: requested table does not have a primary key
```

This error message is from the statement `ALTER TABLE "People" RENAME TO "Persons";`

**Additional context**
This affects efcore.pg tests

@fqazi @rafiss

Jira issue: CRDB-32232

Epic CRDB-60817

Contributor guide

Open the contributing guide

Research direction

Start by running the reported SQL transaction with the People table and confirm that renaming it after dropping its primary key returns SQLSTATE 55C02. Review the affected efcore.pg tests and the ALTER TABLE rename path; done means the table can be renamed and the PK_Persons constraint added successfully in the same migration.

Written by the indexing model from the issue text.

Assessment

Tech stack
sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.