1
0
mirror of synced 2025-02-21 14:43:14 +03:00

Merge pull request #1239 from deeky666/fix-index-duplication

Fix index duplication for unique association join columns
This commit is contained in:
Marco Pivetta 2015-01-09 23:07:50 +01:00
commit be91cc9bb3

View File

@ -487,7 +487,7 @@ class SchemaTool
$foreignClass = $this->em->getClassMetadata($mapping['targetEntity']); $foreignClass = $this->em->getClassMetadata($mapping['targetEntity']);
if ($mapping['type'] & ClassMetadata::TO_ONE && $mapping['isOwningSide']) { if ($mapping['type'] & ClassMetadata::TO_ONE && $mapping['isOwningSide']) {
$primaryKeyColumns = $uniqueConstraints = array(); // PK is unnecessary for this relation-type $primaryKeyColumns = array(); // PK is unnecessary for this relation-type
$this->gatherRelationJoinColumns( $this->gatherRelationJoinColumns(
$mapping['joinColumns'], $mapping['joinColumns'],
@ -495,14 +495,9 @@ class SchemaTool
$foreignClass, $foreignClass,
$mapping, $mapping,
$primaryKeyColumns, $primaryKeyColumns,
$uniqueConstraints,
$addedFks, $addedFks,
$blacklistedFks $blacklistedFks
); );
foreach ($uniqueConstraints as $indexName => $unique) {
$table->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
}
} elseif ($mapping['type'] == ClassMetadata::ONE_TO_MANY && $mapping['isOwningSide']) { } elseif ($mapping['type'] == ClassMetadata::ONE_TO_MANY && $mapping['isOwningSide']) {
//... create join table, one-many through join table supported later //... create join table, one-many through join table supported later
throw ORMException::notSupported(); throw ORMException::notSupported();
@ -514,7 +509,7 @@ class SchemaTool
$this->quoteStrategy->getJoinTableName($mapping, $foreignClass, $this->platform) $this->quoteStrategy->getJoinTableName($mapping, $foreignClass, $this->platform)
); );
$primaryKeyColumns = $uniqueConstraints = array(); $primaryKeyColumns = array();
// Build first FK constraint (relation table => source table) // Build first FK constraint (relation table => source table)
$this->gatherRelationJoinColumns( $this->gatherRelationJoinColumns(
@ -523,7 +518,6 @@ class SchemaTool
$class, $class,
$mapping, $mapping,
$primaryKeyColumns, $primaryKeyColumns,
$uniqueConstraints,
$addedFks, $addedFks,
$blacklistedFks $blacklistedFks
); );
@ -535,16 +529,11 @@ class SchemaTool
$foreignClass, $foreignClass,
$mapping, $mapping,
$primaryKeyColumns, $primaryKeyColumns,
$uniqueConstraints,
$addedFks, $addedFks,
$blacklistedFks $blacklistedFks
); );
$theJoinTable->setPrimaryKey($primaryKeyColumns); $theJoinTable->setPrimaryKey($primaryKeyColumns);
foreach ($uniqueConstraints as $indexName => $unique) {
$theJoinTable->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
}
} }
} }
} }
@ -595,7 +584,6 @@ class SchemaTool
* @param ClassMetadata $class * @param ClassMetadata $class
* @param array $mapping * @param array $mapping
* @param array $primaryKeyColumns * @param array $primaryKeyColumns
* @param array $uniqueConstraints
* @param array $addedFks * @param array $addedFks
* @param array $blacklistedFks * @param array $blacklistedFks
* *
@ -609,7 +597,6 @@ class SchemaTool
$class, $class,
$mapping, $mapping,
&$primaryKeyColumns, &$primaryKeyColumns,
&$uniqueConstraints,
&$addedFks, &$addedFks,
&$blacklistedFks &$blacklistedFks
) { ) {
@ -617,6 +604,7 @@ class SchemaTool
$foreignColumns = array(); $foreignColumns = array();
$fkOptions = array(); $fkOptions = array();
$foreignTableName = $this->quoteStrategy->getTableName($class, $this->platform); $foreignTableName = $this->quoteStrategy->getTableName($class, $this->platform);
$uniqueConstraints = array();
foreach ($joinColumns as $joinColumn) { foreach ($joinColumns as $joinColumn) {
@ -686,6 +674,12 @@ class SchemaTool
} }
} }
// Prefer unique constraints over implicit simple indexes created for foreign keys.
// Also avoids index duplication.
foreach ($uniqueConstraints as $indexName => $unique) {
$theJoinTable->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
}
$compositeName = $theJoinTable->getName().'.'.implode('', $localColumns); $compositeName = $theJoinTable->getName().'.'.implode('', $localColumns);
if (isset($addedFks[$compositeName]) if (isset($addedFks[$compositeName])
&& ($foreignTableName != $addedFks[$compositeName]['foreignTableName'] && ($foreignTableName != $addedFks[$compositeName]['foreignTableName']