annotated icon indicating copy to clipboard operation
annotated copied to clipboard

[Bug]: HasOne overrides nullability of BelongsTo

Open peldax opened this issue 1 year ago • 5 comments

Describe the bug

Hi, I am trying to integrate CycleORM into a Symfony application, in order to replace Doctrine, and I encountered a strange behavior with relations.

I will show you the example first.

I trimmed all the other tables and managed to reproduce on following 4 entities:

#[Entity(database: 'main')]
class Question
{
    #[Column(type: 'primary')]
    private int $id;

    #[BelongsTo(target: QuestionTextual::class, nullable: true)]
    private ?QuestionTextual $questionTextual = null;

    #[BelongsTo(target: QuestionChoice::class, nullable: true)]
    private ?QuestionChoice $questionChoice = null;

    #[BelongsTo(target: QuestionMultichoice::class, nullable: true)]
    private ?QuestionMultichoice $questionMultichoice = null;
}

#[Entity(database: 'main')]
class QuestionChoice
{
    #[Column(type: 'primary')]
    private readonly int $id;

    #[HasOne(target: Question::class)]
    private Question $question;
}

#[Entity(database: 'main')]
class QuestionTextual
{
    #[Column(type: 'primary')]
    private readonly int $id;

    #[HasOne(target: Question::class)]
    private Question $question;
}

#[Entity(database: 'main')]
class QuestionMultichoice
{
    #[Column(type: 'primary')]
    private readonly int $id;

    #[HasOne(target: Question::class)]
    private Question $question;
 }

Those entities result in following database schema:

create table question_choices
(
    id int auto_increment
        primary key
);

create table question_multichoices
(
    id int auto_increment
        primary key
);

create table question_textuals
(
    id int auto_increment
        primary key
);

create table questions
(
    id                     int auto_increment
        primary key,
    questionChoice_id      int not null,
    questionTextual_id     int null,
    questionMultichoice_id int null,
    constraint questions_foreign_questionchoice_id_66745e0c2f565
        foreign key (questionChoice_id) references question_choices (id)
            on update cascade on delete cascade,
    constraint questions_foreign_questionmultichoice_id_66745e0c2f6fc
        foreign key (questionMultichoice_id) references question_multichoices (id)
            on update cascade on delete cascade,
    constraint questions_foreign_questiontextual_id_66745e0c2f607
        foreign key (questionTextual_id) references question_textuals (id)
            on update cascade on delete cascade
);

create index questions_index_questionchoice_id_66745e0c2f53b
    on questions (questionChoice_id);

create index questions_index_questionmultichoice_id_66745e0c2f6e5
    on questions (questionMultichoice_id);

create index questions_index_questiontextual_id_66745e0c2f5ef
    on questions (questionTextual_id);

The strange thing this issue is about is the nullability of questionChoice_id column. For some reason the column is declared as NOT NULL, while others are nullable - and all the columns have identical attribute declaration.

In this usecase the question has 3 possible types and the tables include some additional settings. In the base table the relation is nullable, because only one of the 3 columns is filled. However in the extending tables the column is not null, because it must have a base table paired to it.

There might be some sort of misunderstanding of the CycleORM relations on my side, but it seems strange to me that one column has different nullability than the others. When the HasOne attribute is removed from the entitiy, the nullability is corrected.

Is this a regression?

I cannot tell as I am a new user of Cycle ORM.

To Reproduce

Use the default setup and sync a database with entitiy declaration above.

Expected behaviour

The column should be nullable.

Media prove

No response

Database

MySQL

Your environment

  • OS: PopOS
  • PHP version: 8.3.7
  • Package version:
    • annotated: 4.1
    • database: 2.11
    • orm: 2.8
    • schema-builder: 2.8

Additional context

No response

peldax avatar Jun 20 '24 17:06 peldax