Unnamed Foreign Keys not longer has a shortened hashed name and run into error
Bug Report
| Q | A |
|---|---|
| Version | 3.7.x-dev (d536aced) |
| Previous Version if the bug is a regression | 3.6.7 |
Summary
We previously did run into the existing Schema issue https://github.com/doctrine/orm/issues/12547 after the fixes where merged our CI running on composer config minimum-stability dev still fails with unexpected error.
Current behavior
In Connection.php line 27:
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name
'sn_snippet_dimension_content_excerpt_audience_target_groups_ibfk_1' is
too longmostly related to https://github.com/doctrine/orm/pull/12528 "Avoid adding then dropping FK constraints" changes
Expected behavior
Foreign Keys are this in current DB:
How to reproduce
git clone [email protected]:sulu/sulu.git reproducer-dc-fkn
cd reproducer-dc-fkn
composer config minimum-stability dev
composer update
docker compose up
bin/console doctrine:schema:update --forceThe remaining failure has a different cause. Since #12528 ("Avoid adding then dropping FK constraints", commit d4dc7681), SchemaTool adds association foreign keys through the DBAL Schema::edit() / TableEditor API and no longer passes a constraint name:
// src/Tools/SchemaTool.php
if (method_exists(Schema::class, 'edit')) {
$schema = $schema->edit()->modifyTable(
$fkData['table']->getObjectName(),
static function (TableEditor $tableEditor) use ($fkData): void {
$tableEditor->addForeignKeyConstraint(new ForeignKeyConstraint(
localColumnNames: $fkData['localColumns'],
foreignTableName: $fkData['foreignTableName'],
foreignColumnNames: $fkData['foreignColumns'],
options: $fkData['fkOptions'],
// no `name:` argument -> constraint is emitted unnamed
));
},
)->create();
} else {
// still names the constraint via Table::_generateIdentifierName(..., 'fk', $maxIdentifierLength)
$fkData['table']->addForeignKeyConstraint(
$fkData['foreignTableName'],
$fkData['localColumns'],
$fkData['foreignColumns'],
$fkData['fkOptions'],
);
}
Before #12528 this code path unconditionally called Table::addForeignKeyConstraint(), which fills in a generated FK_ name truncated to the platform's maximum identifier length. Now that name is dropped and the database is left to invent one.
Schema::edit() only exists on doctrine/dbal 4.5.x-dev, so the bug is invisible on released DBAL versions — on 4.4.x the else branch keeps working. It reproduces with doctrine/orm 3.7.x-dev + doctrine/dbal 4.5.x-dev.
On MySQL, an unnamed constraint is auto-named
Expected behavior
SchemaTool should keep naming the constraints as it did before #12528, so that the generated SQL stays identical across DBAL versions and stays within the platform's identifier length limit:
ALTER TABLE sn_snippet_dimension_content_excerpt_audience_target_groups ADD CONSTRAINT FK_87109F587891499D FOREIGN KEY (snippet_dimension_content_id) REFERENCES sn_snippet_dimension_contents (id) ON DELETE CASCADE;
ALTER TABLE sn_snippet_dimension_content_excerpt_audience_target_groups ADD CONSTRAINT FK_87109F5824FF092E FOREIGN KEY
(target_group_id) REFERENCES at_target_groups (id) ON DELETE CASCADE;Source: doctrine/orm