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

Aperta
#33,230 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@matrix-meow ci sta già lavorando.

Dal 25/11/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

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.

Lingua principale
Go
Stelle
1.9k
Fork
312
Merge medio
22h 59m
PR unite (30g)
736

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di matrixorigin/matrixone

Tutte le issue di matrixorigin/matrixone

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.