Auto-generate check constraint name when name is empty#1098
Merged
Conversation
`AddCheckConstraint::build()` defaults a missing name to an empty string, but the adapters only auto-generated a name when it was `null`. Since `Cake\Database\Schema\Constraint` types the name as a non-nullable string, `getName()` never returns `null`, so the auto-generation branch was dead code and an unnamed check constraint produced invalid SQL, e.g. `ADD CONSTRAINT CHECK (...)` which PostgreSQL rejects with `SQLSTATE[42601]: Syntax error at or near "CHECK"`. Treat an empty string the same as `null` in the Postgres, MySQL and SQLite adapters so a name is generated. Add regression tests for the empty-name path and tighten the MySQL/MariaDB expectation now that migrations supplies the generated name explicitly instead of relying on the database fallback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #1097
Problem
Adding a check constraint without an explicit name generates invalid SQL. On PostgreSQL:
emits an empty constraint name:
which PostgreSQL rejects with
SQLSTATE[42601]: Syntax error at or near "CHECK".Root cause
AddCheckConstraint::build()defaults a missing name to an empty string ($options['name'] ?? ''), but every adapter only auto-generates a name when it isnull:Since
Cake\Database\Schema\Constrainttypes the name as a non-nullablestring,getName()never returnsnull. The auto-generation branch was therefore dead code and an unnamed constraint kept its empty name.This has been latent since the constraint value object switched to the core
CheckConstraint(non-nullable name) in 5.0, and became user-visible once the fluentTable::addCheckConstraint()API landed in 5.1, which is the first path that lets a name go unset.The same dead check exists in the Postgres, MySQL and SQLite adapters. SQL Server already throws
BadMethodCallExceptionfor check constraints, so it is unaffected.Fix
Treat an empty string like
nullin all three adapters so a name is generated:Tests
testAddCheckConstraintAutoGeneratesName()for the Postgres and SQLite adapters covering the empty-name path (SQLite fails without the fix - the empty quoted identifier is a syntax error).testAddCheckConstraintWithAutoGeneratedName()to assert the exact generated name. It previously passed even with the bug because the database supplied its own fallback name (check_table2_chk_1on MySQL,CONSTRAINT_1on MariaDB); it now asserts migrations supplies the name explicitly on both.