2017-12-14 20 views
0

때 가지고 내가 내 데이터베이스를 업데이트하려고하면 오류 :교리, ralation의 ManyToOne

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (symfony . #sql-d8c_55 , CONSTRAINT FK_957A6479A233CB39 FOREIGN KEY (klient_id) REFERENCES klient (id))

내 클래스 사용자 :

namespace AppBundle\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use FOS\UserBundle\Model\User as BaseUser; 
use Doctrine\ORM\Mapping as ORM; 


/** 
* Class User 
* @package AppBundle\Entity 
* 
* @ORM\Table("fos_user") 
* @ORM\Entity() 
*/ 
class User extends BaseUser 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 


    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Klient", inversedBy="users") 
    * @ORM\JoinColumn(nullable=false) 
    */ 
    private $klient; 

    /** 
    * @return mixed 
    */ 
    public function getKlient() 
    { 
     return $this->klient; 
    } 

    /** 
    * @param mixed $klient 
    */ 
    public function setKlient($klient) 
    { 
     $this->klient = $klient; 
    } 

    public function __construct() 
    { 
     parent::__construct(); 

    } 
} 

클래스 Klient 내 의견

namespace AppBundle\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* Klient 
* 
* @ORM\Table(name="klient") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\KlientRepository") 
*/ 
class Klient 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="nazwa", type="string", length=255, unique=true) 
    */ 
    private $nazwa; 


    /** 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\User", mappedBy="klient") 
    */ 
    private $users; 

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

    /** 
    * Set nazwa 
    * 
    * @param string $nazwa 
    * 
    * @return Klient 
    */ 
    public function setNazwa($nazwa) 
    { 
     $this->nazwa = $nazwa; 

     return $this; 
    } 

    /** 
    * Get nazwa 
    * 
    * @return string 
    */ 
    public function getNazwa() 
    { 
     return $this->nazwa; 
    } 

    public function __construct() 
    { 
     $this->users = new ArrayCollection(); 
    } 
} 
+1

하시기 바랍니다한다 이러한 엔티티를 지속시키는 것과 관련된 나머지 코드를 보여줍니다. Doctrine은 자동으로 관계를 유지하지 않으므로 (그렇게하도록 구성된 경우는 제외), 그 원인이 될 수 있지만 그 코드를 추가 할 때 더 많이 알 수 있습니다. –

+0

좋아요, 내 게시물을 편집합니다 – pierzcha

+0

엔티티를 실제로 유지하는 코드는 무엇입니까? 엔티티 클래스를 볼 수는 있지만 아무것도하지 않습니다. –

답변

0

이 있습니다 오류는 데이터베이스에 이미 데이터가 있기 때문에 발생합니다. 사용자 테이블에 외래 키를 추가하려고하면 kcient_id가 null입니다. 그리고 정의에서 nullable : false를 지정합니다.

두 번 진행하는 것이 좋습니다. Null 허용하는, 진정한 데이터베이스를 업데이트하고

  • 재 편집하여 주석을 klient하도록 클라이언트를 링크 :

    1. 을 편집 주석이 널 (NULL)로하는 경우는 false가 확인 될