@Column yields errors when using the 'generated' option. #7401

Open
opened 2026-01-22 15:51:17 +01:00 by admin · 5 comments
Owner

Originally created by @whataboutpereira on GitHub (Jul 28, 2024).

Bug Report

Q A
BC Break no
Version 3.2.1

Summary

Using the Column option generated: 'NEVER' yields an error:

Typed property Doctrine\ORM\Mapping\FieldMapping::$generated must not be accessed before initialization

Current behavior

Using the Column option generated: 'NEVER' yields an error and generated: 'INSERT' works unexpectedly.

How to reproduce

Using a database trigger to update a field defined as such:

    #[ORM\Column(
        name: 'UnreadComments',
        options: ['unsigned' => true, 'default' => 0],
        insertable: false,
        updatable: false,
    )]

This yields an error from phpstan (I know I can silence it):

Property App\Entity\User::$unreadComments is never written, only read.

This made me look at the 'generated' option and when adding generated: 'NEVER' to the column options I get the following error:

Typed property Doctrine\ORM\Mapping\FieldMapping::$generated must not be accessed before initialization

When adding generated: 'INSERT' to the column options, phpstan will be silent, but a SELECT is done after UPDATE to the entity.

Expected behavior

generated: 'NEVER' should not yield an error and should not refresh field after update or insert.
generated: 'INSERT' should refresh the field after insert, but not after update?

Documentation says generated: An enum with the possible values ALWAYS, INSERT, NEVER. Is used after an INSERT or UPDATE statement to determine if the database generated this value and it needs to be fetched using a SELECT statement.

What is the default?

Originally created by @whataboutpereira on GitHub (Jul 28, 2024). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | 3.2.1 #### Summary Using the Column option `generated: 'NEVER'` yields an error: `Typed property Doctrine\ORM\Mapping\FieldMapping::$generated must not be accessed before initialization` #### Current behavior Using the Column option `generated: 'NEVER'` yields an error and `generated: 'INSERT'` works unexpectedly. #### How to reproduce Using a database trigger to update a field defined as such: ``` #[ORM\Column( name: 'UnreadComments', options: ['unsigned' => true, 'default' => 0], insertable: false, updatable: false, )] ``` This yields an error from phpstan (I know I can silence it): `Property App\Entity\User::$unreadComments is never written, only read.` This made me look at the 'generated' option and when adding `generated: 'NEVER'` to the column options I get the following error: `Typed property Doctrine\ORM\Mapping\FieldMapping::$generated must not be accessed before initialization` When adding `generated: 'INSERT'` to the column options, phpstan will be silent, but a SELECT is done after UPDATE to the entity. #### Expected behavior `generated: 'NEVER'` should not yield an error and should not refresh field after update or insert. `generated: 'INSERT'` should refresh the field after insert, but not after update? Documentation says generated: An enum with the possible values ALWAYS, INSERT, NEVER. Is used after an INSERT or UPDATE statement to determine if the database generated this value and it needs to be fetched using a SELECT statement. What is the default?
Author
Owner

@cjavad commented on GitHub (Sep 29, 2025):

Same issue in 3.5.1

@cjavad commented on GitHub (Sep 29, 2025): Same issue in 3.5.1
Author
Owner

@stof commented on GitHub (Oct 14, 2025):

This made me look at the 'generated' option and when adding generated: 'NEVER' to the column options I get the following error:

Typed property Doctrine\ORM\Mapping\FieldMapping::$generated must not be accessed before initialization

Please provide a reproducer for this. And also the stack trace. Having an error message without the full info means we are missing lots of useful info to investigate it.

Documentation says generated: An enum with the possible values ALWAYS, INSERT, NEVER. Is used after an INSERT or UPDATE statement to determine if the database generated this value and it needs to be fetched using a SELECT statement.

What is the default?

the default is NEVER, because this is the behavior that makes sense for all fields that are not generated by the database but managed by the ORM. However, when a field value is actually generated by the DB (a rare case among all the usages of the Column attribute), you most likely want to use a different behavior.

@stof commented on GitHub (Oct 14, 2025): > This made me look at the 'generated' option and when adding `generated: 'NEVER'` to the column options I get the following error: > > `Typed property Doctrine\ORM\Mapping\FieldMapping::$generated must not be accessed before initialization` Please provide a reproducer for this. And also the stack trace. Having an error message without the full info means we are missing lots of useful info to investigate it. > Documentation says generated: An enum with the possible values ALWAYS, INSERT, NEVER. Is used after an INSERT or UPDATE statement to determine if the database generated this value and it needs to be fetched using a SELECT statement. > > What is the default? the default is `NEVER`, because this is the behavior that makes sense for all fields that are _not_ generated by the database but managed by the ORM. However, when a field value is _actually_ generated by the DB (a rare case among all the usages of the `Column` attribute), you most likely want to use a different behavior.
Author
Owner

@cjavad commented on GitHub (Oct 14, 2025):

For us it appears as a bug in __sleep iirc, we get it when using Symfony's PhpFileAdapter + cached ClassMetadataFactory, here i trigger the path by invoking orm:validate-schema.

Fatal error: Uncaught Error: Typed property Doctrine\ORM\Mapping\FieldMapping::$generated must not be accessed before initialization in /var/www/test/ecosystem/vendor/doctrine/orm/src/Mapping/FieldMapping.php:166
Stack trace:
#0 /var/www/test/ecosystem/vendor/symfony/var-exporter/Internal/Exporter.php(118): Doctrine\ORM\Mapping\FieldMapping->__sleep()
#1 /var/www/test/ecosystem/vendor/symfony/var-exporter/Internal/Exporter.php(62): Symfony\Component\VarExporter\Internal\Exporter::prepare()
#2 /var/www/test/ecosystem/vendor/symfony/var-exporter/Internal/Exporter.php(62): Symfony\Component\VarExporter\Internal\Exporter::prepare()
#3 /var/www/test/ecosystem/vendor/symfony/var-exporter/Internal/Exporter.php(177): Symfony\Component\VarExporter\Internal\Exporter::prepare()
#4 /var/www/test/ecosystem/vendor/symfony/var-exporter/VarExporter.php(53): Symfony\Component\VarExporter\Internal\Exporter::prepare()
#5 /var/www/test/ecosystem/vendor/symfony/cache/Adapter/PhpFilesAdapter.php(220): Symfony\Component\VarExporter\VarExporter::export()
#6 /var/www/test/ecosystem/vendor/symfony/cache/Adapter/AbstractAdapter.php(163): Symfony\Component\Cache\Adapter\PhpFilesAdapter->doSave()
#7 /var/www/test/ecosystem/vendor/doctrine/persistence/src/Persistence/Mapping/AbstractClassMetadataFactory.php(202): Symfony\Component\Cache\Adapter\AbstractAdapter->commit()
#8 /var/www/test/ecosystem/vendor/doctrine/persistence/src/Persistence/Mapping/AbstractClassMetadataFactory.php(88): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor()
#9 /var/www/test/ecosystem/vendor/doctrine/orm/src/Tools/SchemaValidator.php(90): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getAllMetadata()
#10 /var/www/test/ecosystem/vendor/doctrine/orm/src/Tools/Console/Command/ValidateSchemaCommand.php(47): Doctrine\ORM\Tools\SchemaValidator->validateMapping()
#11 /var/www/test/ecosystem/vendor/symfony/console/Command/Command.php(318): Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand->execute()
#12 /var/www/test/ecosystem/vendor/symfony/console/Application.php(1092): Symfony\Component\Console\Command\Command->run()
#13 /var/www/test/ecosystem/vendor/symfony/console/Application.php(359): Symfony\Component\Console\Application->doRunCommand()
#14 /var/www/test/ecosystem/vendor/symfony/console/Application.php(194): Symfony\Component\Console\Application->doRun()
#15 /var/www/test/ecosystem/vendor/doctrine/orm/src/Tools/Console/ConsoleRunner.php(32): Symfony\Component\Console\Application->run()

With an entity with #[Column(generated: 'NEVER')] but not INSERT or other options.

@cjavad commented on GitHub (Oct 14, 2025): For us it appears as a bug in `__sleep` iirc, we get it when using Symfony's PhpFileAdapter + cached ClassMetadataFactory, here i trigger the path by invoking orm:validate-schema. ``` Fatal error: Uncaught Error: Typed property Doctrine\ORM\Mapping\FieldMapping::$generated must not be accessed before initialization in /var/www/test/ecosystem/vendor/doctrine/orm/src/Mapping/FieldMapping.php:166 Stack trace: #0 /var/www/test/ecosystem/vendor/symfony/var-exporter/Internal/Exporter.php(118): Doctrine\ORM\Mapping\FieldMapping->__sleep() #1 /var/www/test/ecosystem/vendor/symfony/var-exporter/Internal/Exporter.php(62): Symfony\Component\VarExporter\Internal\Exporter::prepare() #2 /var/www/test/ecosystem/vendor/symfony/var-exporter/Internal/Exporter.php(62): Symfony\Component\VarExporter\Internal\Exporter::prepare() #3 /var/www/test/ecosystem/vendor/symfony/var-exporter/Internal/Exporter.php(177): Symfony\Component\VarExporter\Internal\Exporter::prepare() #4 /var/www/test/ecosystem/vendor/symfony/var-exporter/VarExporter.php(53): Symfony\Component\VarExporter\Internal\Exporter::prepare() #5 /var/www/test/ecosystem/vendor/symfony/cache/Adapter/PhpFilesAdapter.php(220): Symfony\Component\VarExporter\VarExporter::export() #6 /var/www/test/ecosystem/vendor/symfony/cache/Adapter/AbstractAdapter.php(163): Symfony\Component\Cache\Adapter\PhpFilesAdapter->doSave() #7 /var/www/test/ecosystem/vendor/doctrine/persistence/src/Persistence/Mapping/AbstractClassMetadataFactory.php(202): Symfony\Component\Cache\Adapter\AbstractAdapter->commit() #8 /var/www/test/ecosystem/vendor/doctrine/persistence/src/Persistence/Mapping/AbstractClassMetadataFactory.php(88): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() #9 /var/www/test/ecosystem/vendor/doctrine/orm/src/Tools/SchemaValidator.php(90): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getAllMetadata() #10 /var/www/test/ecosystem/vendor/doctrine/orm/src/Tools/Console/Command/ValidateSchemaCommand.php(47): Doctrine\ORM\Tools\SchemaValidator->validateMapping() #11 /var/www/test/ecosystem/vendor/symfony/console/Command/Command.php(318): Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand->execute() #12 /var/www/test/ecosystem/vendor/symfony/console/Application.php(1092): Symfony\Component\Console\Command\Command->run() #13 /var/www/test/ecosystem/vendor/symfony/console/Application.php(359): Symfony\Component\Console\Application->doRunCommand() #14 /var/www/test/ecosystem/vendor/symfony/console/Application.php(194): Symfony\Component\Console\Application->doRun() #15 /var/www/test/ecosystem/vendor/doctrine/orm/src/Tools/Console/ConsoleRunner.php(32): Symfony\Component\Console\Application->run() ``` With an entity with `#[Column(generated: 'NEVER')]` but not INSERT or other options.
Author
Owner

@cjavad commented on GitHub (Oct 14, 2025):

I created a minimal reproduction using a blank symfony project here http://github.com/cjavad/doctrine-orm-issue-11559 where bin/console doctrine:schema:validate triggers the error.

symfony new doctrine-orm-issue-11559 --version="7.3.x"
cd doctrine-orm-issue-11559/
composer require symfony/orm-pack
composer require --dev symfony/maker-bundle
# update .env to use sqlite
php bin/console doctrine:database:create
php bin/console make:entity # I created TestEnitity.php
bin/console doctrine:schema:validate # error
@cjavad commented on GitHub (Oct 14, 2025): I created a minimal reproduction using a blank symfony project here http://github.com/cjavad/doctrine-orm-issue-11559 where `bin/console doctrine:schema:validate` triggers the error. ```sh symfony new doctrine-orm-issue-11559 --version="7.3.x" cd doctrine-orm-issue-11559/ composer require symfony/orm-pack composer require --dev symfony/maker-bundle # update .env to use sqlite php bin/console doctrine:database:create php bin/console make:entity # I created TestEnitity.php bin/console doctrine:schema:validate # error ```
Author
Owner

@cjavad commented on GitHub (Oct 23, 2025):

@stof is the above enough / repeatable or do you need something more? lmk

@cjavad commented on GitHub (Oct 23, 2025): @stof is the above enough / repeatable or do you need something more? lmk
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7401