diff --git a/src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php b/src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php
index 01d99067fe..b25bfd6853 100644
--- a/src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php
+++ b/src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php
@@ -134,7 +134,7 @@ protected function addArchiveTable(): void
// see https://github.com/propelorm/Propel/issues/175 for details
foreach ($table->getUnices() as $unique) {
$index = new Index();
- $index->setTable($table);
+ $index->setTable($archiveTable);
foreach ($unique->getColumns() as $columnName) {
if ($size = $unique->getColumnSize($columnName)) {
$index->addColumn(['name' => $columnName, 'size' => $size]);
@@ -142,7 +142,10 @@ protected function addArchiveTable(): void
$index->addColumn(['name' => $columnName]);
}
}
- $archiveTable->addIndex($index);
+
+ if (!$archiveTable->hasIndex($index->getName())) {
+ $archiveTable->addIndex($index);
+ }
}
// every behavior adding a table should re-execute database behaviors
foreach ($database->getBehaviors() as $behavior) {
diff --git a/tests/Propel/Tests/Generator/Behavior/Archivable/ArchivableBehaviorTest.php b/tests/Propel/Tests/Generator/Behavior/Archivable/ArchivableBehaviorTest.php
index 1cd677a924..44bb6cad70 100644
--- a/tests/Propel/Tests/Generator/Behavior/Archivable/ArchivableBehaviorTest.php
+++ b/tests/Propel/Tests/Generator/Behavior/Archivable/ArchivableBehaviorTest.php
@@ -27,6 +27,7 @@
use Map\MyOldArchivableTest3TableMap;
use Propel\Generator\Util\QuickBuilder;
use Propel\Tests\TestCase;
+use function substr_count;
/**
* Tests for ArchivableBehavior class
@@ -83,6 +84,9 @@ public function setUp(): void
+
+
+
@@ -208,6 +212,15 @@ public function testCopiesUniquesToIndices()
$this->assertStringContainsString($expected, self::$generatedSQL);
}
+ /**
+ * @return void
+ */
+ public function testCopiedUniqueDoesNotDuplicateCopiedIndex()
+ {
+ $expectedSqlMigration = 'CREATE INDEX my_old_archivable_test_3_i_639136 ON my_old_archivable_test_3 (title);';
+ $this->assertSame(1, substr_count(self::$generatedSQL, $expectedSqlMigration));
+ }
+
/**
* @return void
*/