Skip to content

Commit

Permalink
feat: allow setting foreign key as deferrable
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Aug 1, 2023
1 parent 710dde8 commit 05d8f4e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Doctrine/ORM/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* onDelete?: string,
* columnDefinition?: string,
* nullable?: bool,
* options?: array<string, mixed>,
* }
* @psalm-type AssociationMapping = array{
* cache?: array,
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Tools/SchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,10 @@ private function gatherRelationJoinColumns(
if (isset($joinColumn['onDelete'])) {
$fkOptions['onDelete'] = $joinColumn['onDelete'];
}

if (isset($joinColumn['options']['deferrable'])) {
$fkOptions['deferrable'] = $joinColumn['options']['deferrable'];
}
}

// Prefer unique constraints over implicit simple indexes created for foreign keys.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Tests\OrmFunctionalTestCase;

use function array_filter;
use function array_values;
use function implode;
use function str_starts_with;

Expand Down Expand Up @@ -51,6 +52,20 @@ public function testUpdateSchemaWithPostgreSQLSchema(): void

self::assertCount(0, $sql, implode("\n", $sql));
}

public function testSetDeferrableForeignKey(): void
{
$schema = $this->getSchemaForModels(
EntityWithSelfReferencingAssociation::class
);

$table = $schema->getTable('postgres.entitywithselfreferencingassociation');
$fks = array_values($table->getForeignKeys());

self::assertCount(1, $fks);

self::assertTrue($fks[0]->getOption('deferrable'));
}
}

/**
Expand Down Expand Up @@ -119,3 +134,26 @@ class DDC1657Avatar
*/
private $pk;
}

/**
* @Entity
*/
class EntityWithSelfReferencingAssociation
{
/**
* Identifier
*
* @var int
* @Id
* @GeneratedValue(strategy="IDENTITY")
* @Column(type="integer", nullable=false)
*/
private $id;

/**
* @var EntityWithSelfReferencingAssociation
* @ManyToOne(targetEntity="EntityWithSelfReferencingAssociation")
* @JoinColumn(options={"deferrable": true})
*/
private $parent;
}

0 comments on commit 05d8f4e

Please sign in to comment.