Skip to content

Commit 473adb1

Browse files
committed
Dynamically recommend SEQUENCE or IDENTITY
With DBAL 3.x, IDENTITY results in SERIAL. With DBAL 4.x, it results in the standard GENERATED BY DEFAULT AS IDENTITY.
1 parent 73288bc commit 473adb1

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

UPGRADE.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ Make sure to use the former when writing a type declaration or an `instanceof` c
99
To keep PHP mapping attributes consistent, order of arguments passed to above attributes has been changed
1010
so `$targetEntity` is a first argument now. This change affects only non-named arguments usage.
1111

12-
## BC BREAK: AUTO keyword for identity generation defaults to IDENTITY for PostgreSQL now
12+
## BC BREAK: AUTO keyword for identity generation defaults to IDENTITY for PostgreSQL when using `doctrine/dbal` 4
1313

14-
When using the AUTO strategy to let Doctrine determine the identity generation mecehanism for
15-
an entity, PostgreSQL now uses IDENTITY instead of SEQUENCE. When upgrading from ORM 2.x
16-
and preference is on keeping the SEQUENCE based identity generation, then configure the ORM
17-
this way:
14+
When using the `AUTO` strategy to let Doctrine determine the identity generation mechanism for
15+
an entity, and when using `doctrine/dbal` 4, PostgreSQL now uses `IDENTITY`
16+
instead of `SEQUENCE`. When upgrading from ORM 2.x and preference is on keeping
17+
the `SEQUENCE` based identity generation, then configure the ORM this way:
1818

1919
```php
2020
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;

lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use function in_array;
3535
use function is_a;
3636
use function is_subclass_of;
37+
use function method_exists;
3738
use function str_contains;
3839
use function strlen;
3940
use function strtolower;
@@ -616,6 +617,13 @@ private function determineIdGeneratorStrategy(AbstractPlatform $platform): int
616617
}
617618
}
618619

620+
$nonIdentityDefaultStrategy = self::NON_IDENTITY_DEFAULT_STRATEGY;
621+
622+
// DBAL 3
623+
if (method_exists($platform, 'getIdentitySequenceName')) {
624+
$nonIdentityDefaultStrategy[Platforms\PostgreSQLPlatform::class] = ClassMetadata::GENERATOR_TYPE_SEQUENCE;
625+
}
626+
619627
foreach (self::NON_IDENTITY_DEFAULT_STRATEGY as $platformFamily => $strategy) {
620628
if (is_a($platform, $platformFamily)) {
621629
return $strategy;

0 commit comments

Comments
 (0)