Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the same unique index name #11273

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Tools/SchemaTool.php
Expand Up @@ -337,7 +337,14 @@ public function getSchemaFromMetadata(array $classes): Schema

if (isset($class->table['uniqueConstraints'])) {
foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) {
$uniqIndex = new Index($indexName, $this->getIndexColumns($class, $indexData), true, false, [], $indexData['options'] ?? []);
$uniqIndex = new Index(
is_numeric($indexName) ? '' : $indexName,
$this->getIndexColumns($class, $indexData),
true,
false,
[],
$indexData['options'] ?? [],
);

foreach ($table->getIndexes() as $tableIndexName => $tableIndex) {
if ($tableIndex->isFulfilledBy($uniqIndex)) {
Expand All @@ -346,7 +353,11 @@ public function getSchemaFromMetadata(array $classes): Schema
}
}

$table->addUniqueIndex($uniqIndex->getColumns(), is_numeric($indexName) ? null : $indexName, $indexData['options'] ?? []);
$table->addUniqueIndex(
$uniqIndex->getColumns(),
$uniqIndex->getName() === '' ? null : $uniqIndex->getName(),
$indexData['options'] ?? [],
);
}
}

Expand Down
84 changes: 84 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/DDC11246Test.php
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\UniqueConstraint;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\OrmFunctionalTestCase;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

#[Group('DDC-11246')]
class DDC11246Test extends OrmFunctionalTestCase
{
/** @param class-string $className */
#[DataProvider('classesWithoutUniqueConstraintName')]
public function testUniqueConstraintWithoutName(string $className): void
{
$em = $this->getTestEntityManager();
$schemaTool = new SchemaTool($em);

$classes = [
$em->getClassMetadata($className),
];

$schema = $schemaTool->getSchemaFromMetadata($classes);

self::assertTrue($schema->hasTable('unique-constraint-without-name'));
$table = $schema->getTable('unique-constraint-without-name');

self::assertCount(2, $table->getIndexes());
self::assertTrue($table->columnsAreIndexed(['name', 'phone']));
}

public static function classesWithoutUniqueConstraintName(): Generator
{
yield 'Entity class that passes fields for the UniqueConstraint attribute.' => [DDC11246FieldsEntity::class];
yield 'Entity class that passes columns for the UniqueConstraint attribute.' => [DDC11246ColumnsEntity::class];
}
}

#[Entity]
#[Table('unique-constraint-without-name')]
#[UniqueConstraint(fields: ['name', 'phone'])]
class DDC11246FieldsEntity
{
#[Id]
#[Column(type: 'integer')]
public int $id;

#[Column(type: 'string')]
public string $name;

#[Column(type: 'string')]
public string $phone;

#[Column(type: 'string')]
public string $email;
}

#[Entity]
#[Table('unique-constraint-without-name')]
#[UniqueConstraint(columns: ['name', 'phone'])]
class DDC11246ColumnsEntity
{
#[Id]
#[Column(type: 'integer')]
public int $id;

#[Column(type: 'string')]
public string $name;

#[Column(type: 'string')]
public string $phone;

#[Column(type: 'string')]
public string $email;
}