0

I've created two Entity, and i try to create a custom relation between them, without using the default syntax.

See :

/**
    * @ORM\Entity(repositoryClass=LandRepository::class)
    * @ORM\HasLifecycleCallbacks()
*/
class Land
{
    /**
        * @ORM\Id()
        * @ORM\GeneratedValue()
        * @ORM\Column(type="integer")
    */
    private $id;
    
    /**
        * @ORM\Column(type="string", length=255)
    */
    private $libelle;
    
    /**
        * @ORM\Column(type="integer")
    */
    private $uid;
    
    /**
        * @ORM\OneToMany(targetEntity=Ride::class, mappedBy="uidparent")
    */
    private $rides;
}

/**
    * @ORM\Entity(repositoryClass=RideRepository::class)
    * @ORM\HasLifecycleCallbacks()
*/
class Ride
{
    /**
        * @ORM\Id()
        * @ORM\GeneratedValue()
        * @ORM\Column(type="integer")
    */
    private $id;
    
    /**
        * @ORM\Column(type="string", length=255)
    */
    private $libelle;
    
    /**
        * @ORM\ManyToOne(targetEntity=Land::class, inversedBy="rides")
        * @ORM\JoinColumn(name="uidparent", referencedColumnName="uid")
    */
    private $uidparent;
}   

Tables are correctly created, but the last instruction have an error. enter image description here

In MySQL, i made some test, and i need to create an index on "uid" column in "land" table. So, i changed the header of my class "Land" to force the index

/**
    * @ORM\Entity(repositoryClass=LandRepository::class)
    * @ORM\Table(name="land",indexes={@ORM\Index(columns={"uid"})})
    * @ORM\HasLifecycleCallbacks()
*/
class Land
{
    / ... /
}

I don't understand why i need to specify this index creation.

I hope to have something like this :

enter image description here

enter image description here

(PS : I know i can use the classic syntax (using in my entity Ride a column auto named "land_id") but I want to understand and master this alternative syntax to manage future complex entities and associations)

Thanks !

yivi
  • 42,438
  • 18
  • 116
  • 138
Captain Nemo
  • 326
  • 2
  • 13
  • Can you show what error you are getting? – Agnohendrix Jul 03 '20 at 07:41
  • An exception occurred while executing 'ALTER TABLE ride ADD CONSTRAINT FK_9B3D7CD0602C80BE FOREIGN KEY (uidparent) REFER ENCES land (uid)': SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint – Captain Nemo Jul 03 '20 at 08:20
  • Have you checked [this](https://stackoverflow.com/a/16969176/4820276) ? – Agnohendrix Jul 03 '20 at 08:27
  • Yes, as said in my initial post, if i create manually an index on "land.uid", the constraint is correctly created. My question is : Why doesn't Doctrine detect the necessity to create itself this index ? A lack of instruction in entity annotation ? – Captain Nemo Jul 03 '20 at 09:26
  • Your problem might be that you're referencing a Foreign Non-Primary Key, [here](https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/limitations-and-known-issues.html#join-columns-with-non-primary-keys) says it is unsupported, maybe that's why it doesn't create the index itself. – Agnohendrix Jul 03 '20 at 09:50

1 Answers1

0

Need to manually specify in Entity header annotation :

@ORM\Table(name="land",indexes={@ORM\Index(columns={"uid"})})
Captain Nemo
  • 326
  • 2
  • 13