[Bug]: sql_require_primary_key does not enforce primary keys on CREATE or ALTER TABLE

Open
#33,230 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@matrix-meow is already working on this.

Since Nov 25, 2026.

Assessment

This issue has not been assessed yet.

Description

kind/bug needs-triage

Description

Tested on MatrixOne commit 568f0f4fdee3c34373f27b04316a2adf7d32a78a.

sql_require_primary_key=1 is accepted and readable but is not enforced. With invisible-primary-key generation explicitly disabled, CREATE TABLE without a primary key and ALTER TABLE ... DROP PRIMARY KEY both succeed and leave a table with no primary key.

Minimal reproduction

DROP DATABASE IF EXISTS repro_require_primary_key;
CREATE DATABASE repro_require_primary_key;
USE repro_require_primary_key;

SET SESSION sql_generate_invisible_primary_key=0;
SET SESSION sql_require_primary_key=1;
SELECT @@sql_require_primary_key,@@sql_generate_invisible_primary_key;
-- ('1','0')

CREATE TABLE no_pk(id INT, v INT); -- unexpectedly succeeds
SHOW CREATE TABLE no_pk;           -- definition has no PRIMARY KEY

CREATE TABLE has_pk(id INT PRIMARY KEY, v INT);
ALTER TABLE has_pk DROP PRIMARY KEY; -- unexpectedly succeeds
SHOW CREATE TABLE has_pk;            -- PRIMARY KEY has been removed

Expected behavior

MySQL's sql_require_primary_key contract rejects statements that create a table without a primary key or alter a table so that it no longer has one. Both operations above should fail with Error 3750 (ER_TABLE_WITHOUT_PK) while the variable is enabled. Setting sql_generate_invisible_primary_key=0 ensures that an automatically generated key cannot satisfy the requirement.

Actual behavior and controls

  • Both prohibited DDL statements succeed, and SHOW CREATE confirms that the resulting tables really have no primary key.
  • Creating a table with an explicit primary key succeeds under the same enabled setting.
  • After the incorrect DROP, ADD PRIMARY KEY succeeds.
  • After SET SESSION sql_require_primary_key=0, creating a table without a primary key is allowed as expected.

Likely root cause

pkg/frontend/variables.go declares sql_require_primary_key as a dynamic variable, but static inspection found no DDL planner/executor consumer that checks whether CREATE or ALTER would leave the table without a primary key. The admission check needs to cover both table creation and primary-key removal.

Dominant language
Go
Stars
1.9k
Forks
312
Avg merge
22h 59m
Merged PRs (30d)
736

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from matrixorigin/matrixone

All issues in matrixorigin/matrixone

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.