Cby
Cby

Reputation: 9

How to solve schema update error with doctrine?

I'm having an issue on a pretty simple application made with Symfony 4.3.4

I just updated an entity and I'm trying to update my database with "doctrine:schema:update --force", but I have here an issue : An exception occurred while executing 'ALTER TABLE lead ADD content LONGTEXT DEFAULT NULL'

Second message : Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lead ADD content LONGTEXT DEFAULT NULL' at line 1

I tried to change the name of the "content" attribute (it's the one I added), but this did not work. Do someone know why this is happening ?

Here is my Lead entity :

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\LeadRepository")
 */
class Lead
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="datetime")
     */
    private $createdAt;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isAuth = false;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Formation", inversedBy="leads")
     * @ORM\JoinColumn(nullable=false)
     */
    private $formation;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $content;

    public function __construct()
    {
        $this->createdAt = new \DateTime();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getCreatedAt(): ?\DateTimeInterface
    {
        return $this->createdAt;
    }

    public function setCreatedAt(\DateTimeInterface $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    public function getIsAuth(): ?bool
    {
        return $this->isAuth;
    }

    public function setIsAuth(bool $isAuth): self
    {
        $this->isAuth = $isAuth;

        return $this;
    }

    public function getFormation(): ?Formation
    {
        return $this->formation;
    }

    public function setFormation(?Formation $formation): self
    {
        $this->formation = $formation;

        return $this;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(?string $content): self
    {
        $this->content = $content;

        return $this;
    }
}

Upvotes: 1

Views: 871

Answers (1)

Adambean
Adambean

Reputation: 1161

"lead" seems to be a reserved keyword for MySQL, so you need to escape it with backticks as follows:

/**
 * @ORM\Entity(repositoryClass="App\Repository\LeadRepository")
 * @ORM\Table(name="`lead`")
 */

You need to do this for other common keywords used as field names such as "order" and "sort" too.

Upvotes: 1

Related Questions