Skip to content

[Bug]: CREATE TABLE on an existing FK-referenced table returns column-not-found and IF NOT EXISTS fails #27708

Closed

Description

@Ariznawlll

Description

When a table is referenced by a foreign key, issuing CREATE TABLE with the same table name returns an unrelated internal column-not-found error instead of table already exists. CREATE TABLE IF NOT EXISTS also fails instead of being a no-op.

Environment

  • Branch: main
  • Commit: ff4270c844c4b630cf1d921da813ba119d5b5e89
  • Deployment: local launch topology, 1 Log Service + 1 TN + 1 CN, fresh data directory
  • Test date: 2026-08-27

Steps to reproduce

drop database if exists meta_dup_fk;
create database meta_dup_fk;

create table meta_dup_fk.parent(id int primary key);
create table meta_dup_fk.child(
  cid int primary key,
  pid int,
  constraint fk_p foreign key(pid) references meta_dup_fk.parent(id)
);

create table meta_dup_fk.parent(x int);
create table if not exists meta_dup_fk.parent(x int);

Actual behavior

Both statements executed after the foreign key is created fail with:

ERROR 1054 (42S22): internal error: column 'id' no exists in table ''

Without the child foreign key, the ordinary duplicate statement correctly returns:

ERROR 1050: table parent already exists

Expected behavior

  • CREATE TABLE meta_dup_fk.parent(x int) should return the normal table-already-exists error.
  • CREATE TABLE IF NOT EXISTS meta_dup_fk.parent(x int) should succeed as a no-op.
  • Existing foreign-key metadata must not be planned against the ignored replacement definition.

Stability and controls

  • Reproducer: 3/3 independent databases.
  • Control without a foreign key: 3/3 returned error 1050 (table parent already exists).
  • IF NOT EXISTS: 3/3 returned the incorrect error 1054.
  • Failure atomicity: after every failed statement, mo_tables still contained exactly one parent, the table still had exactly one visible id column, and mo_foreign_keys still contained exactly one relationship.

Evidence

round 1: no_fk=1050, with_fk=1054, if_not_exists=1054, state=(1 table, 1 column, 1 FK)
round 2: no_fk=1050, with_fk=1054, if_not_exists=1054, state=(1 table, 1 column, 1 FK)
round 3: no_fk=1050, with_fk=1054, if_not_exists=1054, state=(1 table, 1 column, 1 FK)

Code analysis

pkg/sql/plan/build_ddl.go calls getFkReferredToWithCatalogLayout and buildFkDataOfForwardRefer while building the requested replacement table definition. The latter validates existing reverse foreign-key columns against the new definition (x int) before pkg/sql/compile/ddl.go reaches RelationExists and handles IF NOT EXISTS/error 1050. Consequently, the existing referenced column id is looked up in the wrong table definition.

Regression coverage

After the product fix, add a deterministic BVT case covering both ordinary duplicate CREATE TABLE and CREATE TABLE IF NOT EXISTS for a table referenced by single-column and composite foreign keys, followed by catalog and DML checks.

Activity

  1. added this to the MO-202608 milestone on Aug 27, 2026
  2. added
    kind/bug
    on Aug 27, 2026
  3. added theissue type on Aug 27, 2026
  4. Ariznawlll commented on Aug 31, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试通过。

    验证版本:main@09c9a0b。被单列/复合 FK 引用的父表重复 CREATE 正确返回 1050;IF NOT EXISTS 为 no-op,quoted name、非法子表写入、后续合法 FK DML、catalog 状态均正确,连续 3 轮通过。

    已加入 MOTR 回归:https://github.com/matrixorigin/motr/pull/131。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions