h2database / h2database/h2database

CREATE TABLE succeeds or fails depending on the order of the CONSTRAINT statements

Open
#3,961 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
4.6k
Forks
1.3k
Avg merge
1d 2h
Merged PRs (30d)
9

Description

For H2 versions 2.1.210 the following query works:

CREATE TABLE "meta_data"
(
"meta_data_id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
"identifier" VARCHAR(255) NOT NULL,
"parent_identifier" VARCHAR(255),
CONSTRAINT "meta_data_pk" PRIMARY KEY ("meta_data_id"),
CONSTRAINT "meta_data_parent_identifier_fk" FOREIGN KEY ("parent_identifier") REFERENCES "meta_data" ("identifier"),
CONSTRAINT "meta_data_identifier_uc" UNIQUE ("identifier")
);

where the next query fails:

CREATE TABLE "meta_data"
(
"meta_data_id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
"identifier" VARCHAR(255) NOT NULL,
"parent_identifier" VARCHAR(255),
CONSTRAINT "meta_data_pk" PRIMARY KEY ("meta_data_id"),
CONSTRAINT "meta_data_identifier_uc" UNIQUE ("identifier"),
CONSTRAINT "meta_data_parent_identifier_fk" FOREIGN KEY ("parent_identifier") REFERENCES "meta_data" ("identifier")
);

The only difference being the order of the two last CONSTRAINTS.

For H2 versions 1.4.196 both queries succeed.

Is there a reason why second query has stopped working?

Contributor guide

No contributing guide indexed for this repository

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.

Research direction

Start by reproducing both CREATE TABLE statements against H2 2.1.210 and 1.4.196, comparing the result when the UNIQUE and FOREIGN KEY constraints are reordered. Trace the handling of table constraints and verify that equivalent constraint orders produce the same result, with regression coverage demonstrating both statements succeed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, 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.