phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

Autogenerated id column should be positive-int

Open ghost opened this issue 1 year ago • 5 comments

I want to mark a Doctrine entity $id field that is autogenerated by database engine as @var positive-int. However PHPStan complains.

class Entity
{
    /**
     * @var positive-int|null
     */
    #[ORM\Column(type: 'integer', options: ['unsigned' => true])]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
    private ?int $id;
}

PHPStan output: Property App\Rma\Domain\Rma\Rma::$id type mapping mismatch: database can contain int but property expects int<1, max>|null.

ghost avatar May 20 '24 09:05 ghost

['unsigned' => true] does not work for all drivers (e.g. in postgres)

janedbal avatar May 28 '24 14:05 janedbal

['unsigned' => true] does not work for all drivers (e.g. in postgres)

@janedbal how is it related to my issue?

ghost avatar May 30 '24 08:05 ghost

If support would be added, it needs to be driver-aware.

janedbal avatar May 30 '24 08:05 janedbal

Still don't understand. Is there any database that uses non-positive id?

ghost avatar May 30 '24 08:05 ghost

@javaDeveloperKid database will still allow you to write your own value in the field instead of using the default generated value, which is then only constrained by the type of the field (so you can write -2 in an id field if the DB field is a signed integer type)

stof avatar Aug 23 '24 11:08 stof