UniqueIndex attribute on entity can cause some packaged CLI tools to error #7316

Open
opened 2026-01-22 15:49:50 +01:00 by admin · 1 comment
Owner

Originally created by @Firehed on GitHub (Feb 11, 2024).

Bug Report

Q A
BC Break no
Version 3.0.0

Summary

If a table has a UniqueConstraint attribute where there's no name neing explicitly set (e.g. #[Mapping\UniqueConstraint(fields: ['email'])]), several included CLI tools produce an error:

TypeError: Doctrine\DBAL\Schema\Index::__construct(): Argument #1 ($name) must be of type ?string, int given, called in /app/vendor/doctrine/orm/src/Tools/SchemaTool.php on line 341 and defined in /app/vendor/doctrine/dbal/src/Schema/Index.php:42
Stack trace:
#0 /app/vendor/doctrine/orm/src/Tools/SchemaTool.php(341): Doctrine\DBAL\Schema\Index->__construct(0, Array, true, false, Array, Array)
#1 /app/vendor/doctrine/orm/src/Tools/SchemaTool.php(896): Doctrine\ORM\Tools\SchemaTool->getSchemaFromMetadata(Array)
#2 /app/vendor/doctrine/orm/src/Tools/Console/Command/SchemaTool/UpdateCommand.php(78): Doctrine\ORM\Tools\SchemaTool->getUpdateSchemaSql(Array)
#3 /app/vendor/doctrine/orm/src/Tools/Console/Command/SchemaTool/AbstractCommand.php(37): Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand->executeSchemaCommand(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput), Object(Doctrine\ORM\Tools\SchemaTool), Array, Object(Symfony\Component\Console\Style\SymfonyStyle))
#4 /app/vendor/symfony/console/Command/Command.php(279): Doctrine\ORM\Tools\Console\Command\SchemaTool\AbstractCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#5 /app/vendor/symfony/console/Application.php(1031): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#6 /app/vendor/symfony/console/Application.php(318): Symfony\Component\Console\Application->doRunCommand(Object(Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /app/vendor/symfony/console/Application.php(169): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#8 /app/vendor/doctrine/orm/src/Tools/Console/ConsoleRunner.php(30): Symfony\Component\Console\Application->run()
#9 /app/cli(24): Doctrine\ORM\Tools\Console\ConsoleRunner::run(Object(Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider), Array)
#10 {main}

I also tested by replacing the UniqueConstraint with an Index and that has the correct behavior. It appears isolated to unique indexes.

Current behavior

Instead of a deterministic index name being generated from the referenced values, the above error is produced.

How to reproduce

  1. Define an entity with a UniqueConstraint attribute. Both fields and columns definitions reproduce this. Do not explicitly set a name.
  2. Configure Doctrine otherwise per normal.
  3. Run one of the following packaged CLI commands:
  • orm:info
  • orm:validate
  • orm:schema-tool:update --dump-sql

Expected behavior

If name is specified in the attribute, it's used (this IS the current behavior). If not, one is generated based on the columns as occurred in ORM 2.x.

Originally created by @Firehed on GitHub (Feb 11, 2024). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | 3.0.0 #### Summary If a table has a UniqueConstraint attribute where there's no name neing explicitly set (e.g. `#[Mapping\UniqueConstraint(fields: ['email'])]`), several included CLI tools produce an error: ``` TypeError: Doctrine\DBAL\Schema\Index::__construct(): Argument #1 ($name) must be of type ?string, int given, called in /app/vendor/doctrine/orm/src/Tools/SchemaTool.php on line 341 and defined in /app/vendor/doctrine/dbal/src/Schema/Index.php:42 Stack trace: #0 /app/vendor/doctrine/orm/src/Tools/SchemaTool.php(341): Doctrine\DBAL\Schema\Index->__construct(0, Array, true, false, Array, Array) #1 /app/vendor/doctrine/orm/src/Tools/SchemaTool.php(896): Doctrine\ORM\Tools\SchemaTool->getSchemaFromMetadata(Array) #2 /app/vendor/doctrine/orm/src/Tools/Console/Command/SchemaTool/UpdateCommand.php(78): Doctrine\ORM\Tools\SchemaTool->getUpdateSchemaSql(Array) #3 /app/vendor/doctrine/orm/src/Tools/Console/Command/SchemaTool/AbstractCommand.php(37): Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand->executeSchemaCommand(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput), Object(Doctrine\ORM\Tools\SchemaTool), Array, Object(Symfony\Component\Console\Style\SymfonyStyle)) #4 /app/vendor/symfony/console/Command/Command.php(279): Doctrine\ORM\Tools\Console\Command\SchemaTool\AbstractCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #5 /app/vendor/symfony/console/Application.php(1031): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #6 /app/vendor/symfony/console/Application.php(318): Symfony\Component\Console\Application->doRunCommand(Object(Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #7 /app/vendor/symfony/console/Application.php(169): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #8 /app/vendor/doctrine/orm/src/Tools/Console/ConsoleRunner.php(30): Symfony\Component\Console\Application->run() #9 /app/cli(24): Doctrine\ORM\Tools\Console\ConsoleRunner::run(Object(Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider), Array) #10 {main} ``` I also tested by replacing the `UniqueConstraint` with an `Index` and that has the correct behavior. It appears isolated to unique indexes. #### Current behavior Instead of a deterministic index name being generated from the referenced values, the above error is produced. #### How to reproduce 1) Define an entity with a `UniqueConstraint` attribute. Both `fields` and `columns` definitions reproduce this. **Do not** explicitly set a `name`. 2) Configure Doctrine otherwise per normal. 3) Run one of the following packaged CLI commands: - `orm:info` - `orm:validate` - `orm:schema-tool:update --dump-sql` #### Expected behavior If `name` is specified in the attribute, it's used (this IS the current behavior). If not, one is generated based on the columns as occurred in ORM 2.x.
Author
Owner

@BenMorel commented on GitHub (Feb 28, 2024):

We have the same issue, and indeed, adding a name to each UniqueConstraint fixes it.
We're using version 3.0.1.

@BenMorel commented on GitHub (Feb 28, 2024): We have the same issue, and indeed, adding a `name` to each `UniqueConstraint` fixes it. We're using version `3.0.1`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7316