Skip to content

Generates nullable columns incorrectly for HasMany relation #17

Closed
cycle/schema-builder
#86

Description

@roquie

PHP version: 8.0
Package version: v1.0.10

How to reproduce:

  1. User entity:
<?php

declare(strict_types=1);

namespace App\Database;

use App\Database\Typecast\Uuid;
use App\Exception;
use App\Repository;
use App\Database\Column;
use Cycle\Annotated\Annotation as Cycle;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

#[Cycle\Entity(
    repository: Repository\User::class
)]
class User
{
    use Column\Id;

    #[Cycle\Column(type: 'string(64)')]
    public string $username;

    #[Cycle\Relation\HasMany(target: Comment::class)]
    private Collection $comments;

    public function __construct(Uuid $id, string $username, string $password)
    {
        $this->id = $id;
        $this->username = $username;
        $this->comments = new ArrayCollection();
    }

    public function getComments(): Collection
    {
        return $this->comments;
    }
}
  1. Comment entity
<?php

declare(strict_types=1);

namespace App\Database;

use App\Repository;
use App\Database\Column;
use Cycle\Annotated\Annotation as Cycle;

#[Cycle\Entity(
    repository: Repository\Comment::class
)]
class AuthCode
{
    use Column\Id;

    #[Cycle\Relation\BelongsTo(target: User::class, nullable: true)]
    private ?User $user = null;

    #[Cycle\Column(type: 'string')]
    private string $text;

    public function setUser(?User $user): void
    {
        $this->user = $user;
    }

    public function setText(string $value): void
    {
        $this->text = $value;
    }

    public function getText(): string
    {
        return $this->text;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }
}
  1. php app.php cycle:migrate -r

Results:
Screenshot 2021-08-06 at 11 30 17

P.S. Column Id Trait:

<?php
declare(strict_types=1);

namespace App\Database\Column;

use Cycle\Annotated\Annotation as Cycle;
use App\Database\Typecast\Uuid;

trait Id
{
    #[Cycle\Column(type: 'uuid', typecast: Uuid::class, primary: true)]
    private ?Uuid $id = null;

    public function getId(): ?Uuid
    {
        return $this->id;
    }
}

Results expected as nullable: true.

Activity

  1. vvval commented on Aug 6, 2021

    @vvval
    Contributor

    probably this is the same problem as I faced in this issue cycle/orm#120

  2. butschster commented on Aug 17, 2021

    @butschster
    Contributor

    Try to add nullable to comments relation

    #[Cycle\Relation\HasMany(target: Comment::class, nullable: true)]
    private Collection $comments;
    
  3. moved this to Backlog in Cycleon Jan 4, 2022
  4. moved this from Backlog to Todo in Cycleon Jan 11, 2022
  5. moved this from Todo to In Progress in Cycleon Mar 29, 2024
  6. moved this from In Progress to Todo in Cycleon May 4, 2024
  7. roxblnfk commented on Oct 5, 2026

    @roxblnfk
    Member

    Fixed in cycle/schema-builder#86: when several relations generate the same foreign key column (e.g. HasMany and BelongsTo(nullable: true)), the column is now nullable if any of them asks for it, regardless of entity order. Columns declared explicitly via #[Column] are not changed. The fix will be available in the next cycle/schema-builder release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions