#[JoinColumn] changes nullability of a relation #6713

Closed
opened 2026-01-22 15:37:24 +01:00 by admin · 2 comments
Owner

Originally created by @Nemo64 on GitHub (May 10, 2021).

I'm still experimenting with the 2.9.x branch (6753b26) and noticed a strange schema difference.

class Order {
    #[ORM\ManyToOne]
    #[ORM\JoinColumn]
    public ProductVariant $productVariant;
}
class Order {
    #[ORM\ManyToOne]
    public ProductVariant $productVariant;
}

These 2 definitions create a different schema. The JoinColumn definition creates a NOT NULL column definition while the slimmer schema is DEFAULT NULL.

I also tested it with a 2.8.4 and there I have to explicitly use @JoinColumn(nullable=false) to make that column not nullable. The pure presence of the annotation does not change the schema.

I think that's not intended.

Originally created by @Nemo64 on GitHub (May 10, 2021). I'm still experimenting with the 2.9.x branch (6753b26) and noticed a strange schema difference. ```php class Order { #[ORM\ManyToOne] #[ORM\JoinColumn] public ProductVariant $productVariant; } ``` ```php class Order { #[ORM\ManyToOne] public ProductVariant $productVariant; } ``` These 2 definitions create a different schema. The `JoinColumn` definition creates a `NOT NULL` column definition while the slimmer schema is `DEFAULT NULL`. I also tested it with a `2.8.4` and there I have to explicitly use `@JoinColumn(nullable=false)` to make that column not nullable. The pure presence of the annotation does not change the schema. I think that's not intended.
admin closed this issue 2026-01-22 15:37:24 +01:00
Author
Owner

@beberlei commented on GitHub (May 10, 2021):

Yes this is already known, see #8678 please if that works for you.

@beberlei commented on GitHub (May 10, 2021): Yes this is already known, see #8678 please if that works for you.
Author
Owner

@Nemo64 commented on GitHub (May 14, 2021):

I tested the new version and I still have strange behavior.

class A {
    #[ORM\ManyToOne]
    public Region $region; // DEFAULT NULL

    #[ORM\ManyToOne]
    #[ORM\JoinColumn]
    public Region $region; // NOT NULL

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable=true)]
    public Region $region; // NOT NULL
}
  • It seems like the type hint logic only runs when the join column definition exists.
  • If there is at least 1 indicator of nullable=false (be it php type hint or metadata) it always assumes nullable false.

What I would expect:

  • If the metadata specifies nullable, that should overrule everything else. There are some circular situations where it is necessary to have the relation nullable.
  • If I understand #8678 correctly, then a none nullable php property should default to a none nullable schema property
  • and if nothing is specified, it should be nullable.
@Nemo64 commented on GitHub (May 14, 2021): I tested the new version and I still have strange behavior. ```php class A { #[ORM\ManyToOne] public Region $region; // DEFAULT NULL #[ORM\ManyToOne] #[ORM\JoinColumn] public Region $region; // NOT NULL #[ORM\ManyToOne] #[ORM\JoinColumn(nullable=true)] public Region $region; // NOT NULL } ``` - It seems like the type hint logic only runs when the join column definition exists. - If there is at least 1 indicator of nullable=false (be it php type hint or metadata) it always assumes nullable false. What I would expect: - If the metadata specifies `nullable`, that should overrule everything else. There are some circular situations where it is necessary to have the relation nullable. - If I understand #8678 correctly, then a none nullable php property should default to a none nullable schema property - and if nothing is specified, it should be nullable.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6713