Metadata field type validation against Entity property type does not detect nullability #7422

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

Originally created by @AlexMinaev19 on GitHub (Oct 2, 2024).

Bug Report

In feature request there was added an ability to validate field type against Entity property type.

Schema validation tool does not detect mapping problems when a typed property of the entity defined as nullable, but there is no nullable: true in the column metadata definition.

Q A
BC Break no
Version 2.19.7

Summary

For example, we have the following entity:

class User
{
    // ...

    #[ORM\Column(type: Types::STRING, length: 64)]
    private ?string $name;

    // ...
}

Schema validation tool should report, that mapping is invalid due to property is type string|null, but metadata mapping expecting string due to nullable named property of ORM\Column is false by default. The error should gone when manually will be specified nullable: true of ORM\Column.

Current behavior

There are no errors for provided example class, but should be.

How to reproduce

Create entity:


namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table('user')]
class User
{
    #[ORM\Column(type: Types::STRING, length: 64)]
    private ?string $name;

    public function __construct(?string $name)
    {
         $this->name = $name;
    }

    public function getName(): ?string
    {
         return $this->name;
    }
}

Create migration based on this entity. The table definition in migration will have a non-nullable column name. After that, please go ahead and execute this migration. And then run orm:validate-schema.

Expected behavior

The command should report mapping error:

Mapping
-------

 [FAIL] The entity-class App\Entity\User mapping is invalid:
 * The field 'App\Entity\User#name' has the property type 'string|null' that differs from the metadata field type 'string' returned by the 'string' DBAL type.

Originally created by @AlexMinaev19 on GitHub (Oct 2, 2024). ### Bug Report In [feature request](https://github.com/doctrine/orm/issues/10661) there was added an ability to validate field type against Entity property type. Schema validation tool does not detect mapping problems when a typed property of the entity defined as nullable, but there is no `nullable: true` in the column metadata definition. | Q | A |------------ | ------ | BC Break | no | Version | 2.19.7 #### Summary For example, we have the following entity: ```php class User { // ... #[ORM\Column(type: Types::STRING, length: 64)] private ?string $name; // ... } ``` Schema validation tool should report, that mapping is invalid due to property is type `string|null`, but metadata mapping expecting `string` due to `nullable` named property of `ORM\Column` is `false` by default. The error should gone when manually will be specified `nullable: true` of `ORM\Column`. #### Current behavior There are no errors for provided example class, but should be. #### How to reproduce Create entity: ```php namespace App\Entity; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table('user')] class User { #[ORM\Column(type: Types::STRING, length: 64)] private ?string $name; public function __construct(?string $name) { $this->name = $name; } public function getName(): ?string { return $this->name; } } ``` Create migration based on this entity. The table definition in migration will have a non-nullable column `name`. After that, please go ahead and execute this migration. And then run `orm:validate-schema`. #### Expected behavior The command should report mapping error: ``` Mapping ------- [FAIL] The entity-class App\Entity\User mapping is invalid: * The field 'App\Entity\User#name' has the property type 'string|null' that differs from the metadata field type 'string' returned by the 'string' DBAL type. ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7422