Reputation: 1246
I have two entities:
/**
* Course
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\CourseRepository")
*/
class Course
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \Doctrine\Common\Collections\ArrayCollection()
*
* @ORM\OneToMany(targetEntity="AppBundle\Entity\TimeTableEntry", mappedBy="course", cascade={"remove"}, orphanRemoval=true)
*
*/
private $timeTableEntries;
/**
* @var boolean
*
* @ORM\Column(name="enabled", type="boolean", nullable=true)
*/
private $enabled;
and
/**
* TimeTableEntry
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\TimeTableEntryRepository")
*/
class TimeTableEntry
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \Doctrine\Common\Collections\ArrayCollection()
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Course", inversedBy="timeTableEntries", cascade={"ALL"})
* @ORM\JoinColumn(name="course_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
private $course;
As you can see I tried with cascade={"ALL"}, onDelete="CASCADE" and orphanRemoval=true.
When I want to delete the Course Entity I get the error message:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (
edutalk
.teaching_event
, CONSTRAINTFK_F2B1088B57042871
FOREIGN KEY (time_table_entry_id
) REFERENCEStime_table_entry
(id
))
What can I try to resolve this?
Upvotes: 0
Views: 1800
Reputation: 414
I cannot comment on your post, so I post it as an answer.
Your problem is that you have another entity TeachingEvent
with a reference to TimeTableEntry
. When you try to delete a TimeTableEntry
, it is impossible because this time_table_entry_id
is still referenced as a foreign key in the TeachingEvent
table.
Before deleting the TimeTableEntry
, first you should unlink it from the TeachingEvent
. I don't have your code, but depending on how TimeTableEntry
and TeachingEvent
relate to each other, it's probably something like:
$teachingEvent->setTimeTableEntry(null);
or
$timeTableEntry->setTeachingEvent(null);
Upvotes: 2