Skip to content

Render an unnamed Index as INDEX (col) instead of throwing - #269

Merged
simon-mundy merged 3 commits into
php-db:0.6.xfrom
simon-mundy:fix/265-unnamed-index
Dec 1, 2026
Merged

simon-mundy merged 3 commits into
php-db:0.6.xfrom
simon-mundy:fix/265-unnamed-index

Conversation

@simon-mundy

Copy link
Copy Markdown
Member

Fixes #265

Problem

new Index('email') with no name fails when rendered, because Index::getExpressionData() always wraps the name in an Identifier, which rejects an empty string. laminas-db rendered INDEX (`email`), which MySQL accepts by generating the index name.

Fix

  • When the name is empty (left out, null or ''), Index renders INDEX ("email"), leaving out the name and its placeholder. Lengths and USING types are kept.
  • Named indexes render exactly as before.
  • docs/book/sql-ddl/constraints.md shows an unnamed index and notes that the name is optional.

What stays the same

  • UniqueKey, PrimaryKey, ForeignKey and Check already left out CONSTRAINT for an empty name and are not touched.
  • php-db/phpdb-mysql renders constraints through core, so this applies to MySQL unchanged.

Tests

  • IndexTest: a data provider of seven unnamed variants (no name, null, '', with lengths, several columns) checks the expression data and the rendered SQL in both CREATE TABLE and ALTER TABLE ... ADD, with regression tests for named indexes.
  • The 22 new tests fail without the fix. Unit suite: 2898 tests pass; Index has 100% line and branch coverage. mago lint is clean and mago analyze reports nothing new.
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

The Index constructor's name is optional, but getExpressionData()
always wrapped the name in an Identifier, so an index built without a
name, or with an empty one, threw "Identifier "" is empty" when a
CREATE TABLE or ALTER TABLE was rendered. laminas-db rendered these as
INDEX (col) and let the database generate the name.

Drop the name placeholder from the specification when the name is
empty. Column lengths and the USING type render as before, and named
indexes are unchanged. Document the name as optional.

Fixes php-db#265

Signed-off-by: Simon Mundy <simon.mundy@peptolab.com>
@simon-mundy
simon-mundy force-pushed the fix/265-unnamed-index branch from 38ff3a4 to 249d63c Compare December 1, 2026 02:38
@simon-mundy simon-mundy added this to the 0.6.0 milestone Dec 1, 2026
@github-project-automation github-project-automation Bot moved this to Todo in @phpdb Dec 1, 2026
@simon-mundy simon-mundy added bug Something isn't working documentation Improvements or additions to documentation qa Improvements in quality assurance of the project labels Dec 1, 2026
@simon-mundy simon-mundy self-assigned this Dec 1, 2026
@simon-mundy
simon-mundy requested a review from tyrsson December 1, 2026 02:51
@github-project-automation github-project-automation Bot moved this from Todo to In Progress in @phpdb Dec 1, 2026
@simon-mundy
simon-mundy merged commit daffcff into php-db:0.6.x Dec 1, 2026
15 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in @phpdb Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation qa Improvements in quality assurance of the project

2 participants