Doctrine overwrite column value for composite key

Viewed 72

I need a relation uniderect one-to-one through composite key. I am using PostgreSQL.

#[Entity()]
#[Table(name: "passengers")]
class Passenger
{
    #[Id]
    #[Column(type: "uuid")]
    private string $id;

    #[Id]
    #[Column(type: "uuid")]
    private string $projectId;

    #[OneToOne(targetEntity: "Ticket", cascade: ["persist"])]
    #[JoinColumn(name: "ticket_id", referencedColumnName: "id")]
    #[JoinColumn(name: "project_id", referencedColumnName: "project_id")]
    private ?Ticket $ticket;

    /**
     * @return string
     */
    public function getId(): string
    {
        return $this->id;
    }

    /**
     * @param string $id
     */
    public function setId(string $id): void
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getProjectId(): string
    {
        return $this->projectId;
    }

    /**
     * @param string $projectId
     */
    public function setProjectId(string $projectId): void
    {
        $this->projectId = $projectId;
    }

    /**
     * @return Ticket|null
     */
    public function getTicket(): ?Ticket
    {
        return $this->ticket;
    }

    /**
     * @param Ticket|null $ticket
     */
    public function setTicket(?Ticket $ticket): void
    {
        $this->ticket = $ticket;
    }
}

#[Entity()]
#[Table(name: "tickets")]
class Ticket
{
    #[Id]
    #[Column(type: Types::STRING)]
    private string $id;

    #[Id]
    #[Column(type: "uuid")]
    private string $projectId;

    /**
     * @return string
     */
    public function getId(): string
    {
        return $this->id;
    }

    /**
     * @param string $id
     */
    public function setId(string $id): void
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getProjectId(): string
    {
        return $this->projectId;
    }

    /**
     * @param string $projectId
     */
    public function setProjectId(string $projectId): void
    {
        $this->projectId = $projectId;
    }
}

I'm trying to persist Passenger entity without Ticket entity:

$passenger = new Passenger();
$passenger->setId('07f5f7c8-def7-4fad-a146-b573421d8832');
$passenger->setProjectId('48ae51a1-4405-4f1d-8061-55a732d383a1');
$this->entityManager->persist($passenger);
$this->entityManager->flush();

I get an error:

Doctrine\DBAL\Exception\NotNullConstraintViolationException : An exception occurred while executing 'INSERT INTO passengers (id, project_id, ticket_id) VALUES (?, ?, ?)' with params ["07f5f7c8-def7-4fad-a146-b573421d8832", null, null]:

SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column "project_id" of relation "passengers" violates not-null constraint
DETAIL:  Failing row contains (07f5f7c8-def7-4fad-a146-b573421d8832, null, null).

I want to draw your attention to the fact that doctrine in sql overwrite the value of project_id for composite key to null.

If I persist Passenger by specifying the Ticket, everything works without errors.

Is this a doctrine bug or am I doing something wrong?

0 Answers
Related