Reputation: 11743
I'm attempting to add a many-to-one relationship between fields in two doctrine entities. I have a facebook feed table, which lists facebook feeds and their corresponding facebook IDs, and I have a FbPageLikes entity, which is a table where I record rows of the number of likes for these facebook feeds over time.
I believe I have annotated correctly, yet I still see this error when attempting to migrate: General error: 1215 Cannot add foreign key constraint
Is there something wrong with my annotations? How can I debug this doctrine migration?
FbPageLikes entity:
/**
* @var $facebookId
*
* @ORM\ManyToOne(targetEntity="Feed", inversedBy="fbPageLikes")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="facebook_id", referencedColumnName="facebookId", onDelete="CASCADE")
* })
*/
private $facebookId;
Feed entity:
/**
* @var integer
*
* @ORM\Column(name="facebookId", type="bigint", nullable=true)
*/
private $facebookId;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="FbPageLikes", mappedBy="facebookId", fetch="EXTRA_LAZY")
*/
private $fbPageLikes;
Upvotes: 0
Views: 452
Reputation: 11743
The reason why this setup failed was because of the annotations on the FbPageLikes
entity. The referencedColumnName
field must refer the the primary key in the corresponding entity Feed
. http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-joincolumn
In my case, I had to set referencedColumnName
to refer to the id
column of the Feed
entity, instead of to the facebookId
column.
Upvotes: 1