cycle / database

Database Abstraction Layer, Schema Introspection, Schema Generation, Query Builders
MIT License
53 stars 22 forks source link

Serial type of field in entities ignores `nullable: true` annotation #156

Open lotyp opened 5 months ago

lotyp commented 5 months ago

No duplicates 🥲.

What happened?

I'm trying to add a SERIAL field as incremental_id. When creating a table, if I set nullable: true, it seems that this annotation is ignored.

Example:

#[Entity(repository: CheckoutRepository::class)]
class Checkout
{
    #[Column(type: 'serial', nullable: true, unique: true)]
    public ?int $incrementalId = null;

    #[Column(type: 'uuid', primary: true, typecast: [CheckoutId::class, 'castValue'], unique: true)]
    private CheckoutId $id;

    // ...

When migrations are generated, in code I see:

$this->table('checkouts')
->addColumn('id', 'uuid', ['nullable' => false, 'defaultValue' => null, 'unique' => true])
->addColumn('incremental_id', 'serial', ['nullable' => false, 'defaultValue' => null])

Version

PHP 8.2

cycle/migrations v4.2.3
cycle/annotated: v3.4.0
cycle/database: v2.7.1
cycle/orm v2.6.1

Initial PR, that added support for SERIAL:

https://github.com/cycle/database/pull/106/files

lotyp commented 5 months ago

There is also no possibility to use Serial in default way.

Example:

#[Entity(repository: CheckoutRepository::class)]
#[Uuid7(field: 'id', column: 'id')]
class Checkout implements AggregateRoot, AuditableEntity
{
    use AggregatableRoot;
    use HasSignatures;

    #[Column(type: 'serial', unique: true)]
    public ?int $incrementalId = null;

    #[Column(type: 'uuid', primary: true, typecast: [CheckoutId::class, 'castValue'], unique: true)]
    private CheckoutId $id;

Will give this error:

SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column "incremental_id" of relation "checkouts" violates not-null constraint
DETAIL:  Failing row contains (018d5fef-2e65-7042-bea7-139479dae10d, null, pending, yortiz@lehner.org, 018d5fef-2cb4-7019-a51a-fef21eca9076, null, {"firstName":"Clara","lastName":"Botsford","middleName":null}, Gleichner-Keebler, +17144848501, {"country":"SA","state":"Texas","city":"Port Jacklyn","postCode"..., {"firstName":"Terrence","lastName":"Larkin","middleName":null}, Mueller, Fritsch and Hyatt, +12183760708, {"country":"GF","state":"Delaware","city":"North Clemmie","postC..., null, null, [], 2024-01-31 14:32:07, {"id":"018d5fef-2e65-7042-bea7-13947ac6ed85","party":"random-par..., 2024-01-31 14:32:07, {"id":"018d5fef-2e65-7042-bea7-13947ac6ed85","party":"random-par..., null, null).