doctrine / DoctrineBundle

Symfony Bundle for Doctrine ORM and DBAL
https://www.doctrine-project.org/projects/doctrine-bundle.html
MIT License
4.7k stars 449 forks source link

PostrgreSQL 15 with Symfony 6.4 ID generation deprecation - SEQUENCE for Doctrine\DBAL\Platforms\PostgreSqlPlatform #1740

Closed Mepcuk closed 8 months ago

Mepcuk commented 8 months ago

I have deprecation warning as below. But how to fix it?

13:32:41 INFO      [deprecation] User Deprecated: Relying on non-optimal defaults for ID generation is deprecated, and IDENTITY
results in SERIAL, which is not recommended.
Instead, configure identifier generation strategies explicitly through
configuration.
We currently recommend "SEQUENCE" for "Doctrine\DBAL\Platforms\PostgreSqlPlatform", so you should use
$configuration->setIdentityGenerationPreferences([
    "Doctrine\DBAL\Platforms\PostgreSqlPlatform" => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
]); (ClassMetadataFactory.php:751 called by ClassMetadataFactory.php:625, https://github.com/doctrine/orm/issues/8893, package doctrine/orm) ["exception" => ErrorException { …}]
PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 18221200 bytes) in /var/www/vendor/doctrine/dbal/src/Driver/PDO/Result.php on line 103
Mepcuk commented 8 months ago

Found solution -> in your Entity's id field need to specify strategy 'SEQUENCE'

#[ORM\Entity(repositoryClass: WebpageRepository::class)]
#[ORM\Table(name: '`webpage`')]
class Webpage
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'SEQUENCE')]
    #[ORM\Column]
    private ?int $id = null;
....
Mepcuk commented 8 months ago

Solved - more in docs https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/basic-mapping.html#identifier-generation-strategies

DannyvdSluijs commented 8 months ago

@Mepcuk Are your sure? I'm experiencing the same issue and based on the code in the ClassMetadataFactory on line 750 (src) it always throws an deprecation.

This is also what I experience when trying different values. And as far as I can see this library doesn't support the setIdentityGenerationPreferences method yet.

As a side remark, one thing I found is that this deprecation only happens when an association mapping is part of the entity.

TeLiXj commented 8 months ago

I'm with @DannyvdSluijs, this bundle can't set the setIdentityGenerationPreferences and entities generated by Symfony Maker don't use any default strategies. To fix it you can override the default config using compiler pass in the src\Kernel.php

<?php

namespace App;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Mapping\ClassMetadata;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\{Compiler\CompilerPassInterface, ContainerBuilder};
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel implements CompilerPassInterface
{
    use MicroKernelTrait;

    public function process(ContainerBuilder $container): void
    {
        $container
            ->getDefinition("doctrine.orm.configuration")
            ->addMethodCall("setIdentityGenerationPreferences", [[PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_SEQUENCE]]);
    }
}