简体   繁体   中英

Column name `id` referenced for relation from App\Entity\Students towards App\Entity\Classes does not exist

In my code, a class has many students. Each student has only one class.

I'm trying to update my database schema using php bin/console doctrine:schema:update --force but I keep getting the same error:

Column name id referenced for relation from App\Entity\Students towards App\Entity\Classes does not exist.

How can I fix it?

Student class:

<?php

namespace App\Entity;

use App\Repository\StudentsRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=StudentsRepository::class)
 */
class Students
{

    /**
     * @ORM\id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $studentID;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Classes", inversedBy="students")
     */
    private $classe;
   
  
    public function getclasse(): ?Classes
    {
        return $this->classe;
    }
    public function setclasse(?Classes $classe): self
    {
        $this->classes = $classe; 

        return $this;
    }

    
    public function getstudentID(): ?int
    {
        return $this->studentID;
    }

}

Classes class:

<?php

namespace App\Entity;

use App\Repository\ClassesRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=ClassesRepository::class)
 */
class Classes
{
     /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $classID;

    /**
      * @ORM\OneToMany(targetEntity="App\Entity\Students", mappedBy="classe")
      */
      private $students;

     public function __construct()
     {
         $this->students = new ArrayCollection();
     }

     /**
      * @return Collection/Students[]
      */
      public function getstudents(): Collection
      {
          return $this->students;
      }

I'm not familiar with doctrine, but it looks like you are trying to do a bidirectional relationship. According to the docs, for a bidirectional one-to-many relationship :

This bidirectional mapping requires the mappedBy attribute on the one side and the inversedBy attribute on the many side.


class Students {
    /**
     *
     * @ManyToOne(targetEntitity="App\Entity\Classes", mappedBy="students")
     * @JoinColumn(name="class_id", referencedColumnName="class_id")
     */
    private $classes;
}

class Classes {
   /**
     * @OneToMany(targetEntitity="App\Entity\Students", mappedBy="classes")
     */
    private $students;
}


Note the @joinColumn on the students. I believe that you are changing the default primary key by specifying $studentID on your students class and $classID on your classes class. Therefore you have to specify what column you're joining on.


The following is opinionated so take it with a grain of salt...

FWIW, I would recommend using the default primary key, it is typically what people do (I would say it is industry standard/expected but I am sure people disagree).

Also, based on the names of your entities I would have expected it to be a many-to-many relation; however, I don't know your specific specs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM