Self referencing associate results in Not null violation #6948

Open
opened 2026-01-22 15:42:01 +01:00 by admin · 0 comments
Owner

Originally created by @NotionCommotion on GitHub (Mar 11, 2022).

Bug Report

Q A
BC Break yes/no
Version 2.11.1

Summary

Unable to persist an entity with a not-null self referencing association, yet I could do so doing directly with PDO.

Current behavior

Receive a not null violation even though the entity's value is not null.

How to reproduce

        $pdo = $this->entityManager->getConnection()->getWrappedConnection();

        $pdo->beginTransaction();       
        $pdo->exec('INSERT INTO testing(id, child_obj_id) VALUES(1,1)');
        $pdo->commit();

        $repository = $this->entityManager->getRepository('\App\Entity\Testing\Testing');
        $obj = $repository->find(1);
        printf('Test 1: $obj->getId(): %s $obj->getChildObj()->getId(): %s'.PHP_EOL, $obj->getId(), $obj->getChildObj()->getId());

        $this->entityManager->getConnection()->beginTransaction();
        $obj = new \App\Entity\Testing\Testing;
        $obj->setId(2)->setChildObj($obj);
        $this->entityManager->persist($obj);
        $this->entityManager->getConnection()->commit();
        try{
            $this->entityManager->flush();
        }
        catch(\Exception $exception) {
            printf('Test 2: $obj->getId(): %s $obj->getChildObj()->getId(): %s'.PHP_EOL, $obj->getId(), $obj->getChildObj()->getId());
            exit($exception->getMessage());
        }
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Testing
{
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    private int $id;

    #[ORM\ManyToOne(targetEntity: Testing::class)]
    #[ORM\JoinColumn(nullable: false)]
    private $childObj;

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

    public function setId(int $id): self
    {
        $this->id = $id;
        return $this;
    }

    public function getChildObj(): self
    {
        return $this->childObj;
    }

    public function setChildObj(self $childObj): self
    {
        $this->childObj = $childObj;
        return $this;
    }
}

Output:

Test 1: $obj->getId(): 1 $obj->getChildObj()->getId(): 1
Test 2: $obj->getId(): 2 $obj->getChildObj()->getId(): 2
An exception occurred while executing a query: SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column "child_obj_id" of relation "testing" violates not-null constraint
DETAIL:  Failing row contains (2, null).

Expected behavior

Either a successful insert or at a minimum, a foreign key constraint as not all databases support deferrable constrains (note that I am using PostgreSQL).

Originally created by @NotionCommotion on GitHub (Mar 11, 2022). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | yes/no | Version | 2.11.1 #### Summary Unable to persist an entity with a not-null self referencing association, yet I could do so doing directly with PDO. #### Current behavior Receive a not null violation even though the entity's value is not null. #### How to reproduce ```php $pdo = $this->entityManager->getConnection()->getWrappedConnection(); $pdo->beginTransaction(); $pdo->exec('INSERT INTO testing(id, child_obj_id) VALUES(1,1)'); $pdo->commit(); $repository = $this->entityManager->getRepository('\App\Entity\Testing\Testing'); $obj = $repository->find(1); printf('Test 1: $obj->getId(): %s $obj->getChildObj()->getId(): %s'.PHP_EOL, $obj->getId(), $obj->getChildObj()->getId()); $this->entityManager->getConnection()->beginTransaction(); $obj = new \App\Entity\Testing\Testing; $obj->setId(2)->setChildObj($obj); $this->entityManager->persist($obj); $this->entityManager->getConnection()->commit(); try{ $this->entityManager->flush(); } catch(\Exception $exception) { printf('Test 2: $obj->getId(): %s $obj->getChildObj()->getId(): %s'.PHP_EOL, $obj->getId(), $obj->getChildObj()->getId()); exit($exception->getMessage()); } ``` ```php use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Testing { #[ORM\Id] #[ORM\Column(type: 'integer')] private int $id; #[ORM\ManyToOne(targetEntity: Testing::class)] #[ORM\JoinColumn(nullable: false)] private $childObj; public function getId(): int { return $this->id; } public function setId(int $id): self { $this->id = $id; return $this; } public function getChildObj(): self { return $this->childObj; } public function setChildObj(self $childObj): self { $this->childObj = $childObj; return $this; } } ``` Output: ``` Test 1: $obj->getId(): 1 $obj->getChildObj()->getId(): 1 Test 2: $obj->getId(): 2 $obj->getChildObj()->getId(): 2 An exception occurred while executing a query: SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "child_obj_id" of relation "testing" violates not-null constraint DETAIL: Failing row contains (2, null). ``` #### Expected behavior Either a successful insert or at a minimum, a foreign key constraint as not all databases support deferrable constrains (note that I am using PostgreSQL).
admin added the Bug label 2026-01-22 15:42:01 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6948