'Doctrine - The referenced column name 'id' has to be a primary key
Hy, I have problem with validation and schema creation I am creating an api via the api platform, (this is my 1st project under symfony) I have a constraint, the database exists and I cannot touch it.
I have an headerOrder entity and an LineOrder entity. But the column of join are not a key.
class enteteCommande
{
/**
* @var int
*
* @ORM\Column(name="I_ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $IId;
/**
* @var string
*
* @ORM\Column(name="C_CDE_NUMERO", type="string", length=50, nullable=true)
*
*/
#[Groups(['write:commande'])]
private $CCdeNumero;
/**
*
* @ORM\ManyToMany(targetEntity="lignesCommande", mappedBy="enteteLigne")
*
*/
private $detailLigne;
class lignesCommande
{
/**
* @varint
*
* @ORM\Column(name="I_IDL", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $IIdL;
/**
* @varstring|null
*
* @ORM\Column(name="LIGNE_C_CDE_NUMERO", type="string", length=50, nullable=true)
*/
private $ligneCCdeNumero;
/**
*
*
* @ORM\ManyToMany(targetEntity="enteteCommande", inversedBy="detailLigne")
* @ORM\JoinColumn(name="LIGNE_C_CDE_NUMERO", referencedColumnName="C_CDE_NUMERO")
*
*/
private $enteteLigne;
My schema :
enteteCommande |
---|
I_ID |
C_CDE_NUMERO |
lignesCommande |
---|
I_IDL |
LIGNE_C_CDE_NUMERO |
And error log : The referenced column name 'id' has to be a primary key column on the target entity class 'App\Entity\OrderLignes'. The referenced column name 'id' has to be a primary key column on the target entity class 'App\Entity\OrderEntete'.
How to make manytomany without key ?
thank !
Solution 1:[1]
You need to configure the JoinTable on the owning side of the relationship (lignesCommande
). Not just a JoinColumn
. If the JoinTable
configuration is missing, Doctrine will fall back to its default ManyToMany
configuration and try to connect id
primary key columns, which in your case won't work.
Here's an example (but untested) JoinTable
annotation:
/**
* @ORM\ManyToMany(
* targetEntity="enteteCommande",
* inversedBy="detailLigne"
* )
* @ORM\JoinTable(
* name="foobar",
* joinColumns={
* @ORM\JoinColumn(
* name="LIGNE_C_CDE_NUMERO",
* referencedColumnName="LIGNE_C_CDE_NUMERO"
* ),
* },
* inverseJoinColumns={
* @ORM\JoinColumn(
* name="C_CDE_NUMERO",
* referencedColumnName="C_CDE_NUMERO"
* ),
* }
* )
*/
private $enteteLigne;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jeroen van der Laan |