【问题标题】:How to solve schema update error with doctrine?如何使用学说解决架构更新错误?
【发布时间】:2020-05-11 14:55:23
【问题描述】:

我在使用 Symfony 4.3.4 制作的一个非常简单的应用程序时遇到问题

我刚刚更新了一个实体,我正在尝试使用“doctrine:schema:update --force”来更新我的数据库,但我遇到了一个问题:An exception occurred while executing 'ALTER TABLE lead ADD content LONGTEXT DEFAULT NULL'

第二条消息: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

我尝试更改“内容”属性的名称(这是我添加的),但这不起作用。有人知道为什么会这样吗?

这是我的主要实体:

<?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;
    }
}

【问题讨论】:

    标签: database symfony doctrine


    【解决方案1】:

    "lead" 似乎是 MySQL 的保留关键字,因此您需要使用反引号将其转义,如下所示:

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

    您也需要对用作字段名称的其他常用关键字(例如“order”和“sort”)执行此操作。

    【讨论】:

    • 它似乎适用于某些项目,但这对我不起作用。我最终不得不重命名我的整个实体以使其正常工作。幸运的是,我的项目还很年轻。感谢您的帮助。
    • 您是否将“ORM\Table”行添加到您的实体类中,就像我写的那样,在“lead”周围加上反引号,然后运行架构更新?当你这样做时发生了什么?
    • 是的,我就是这样做的,但是我得到了同样的错误信息。我尝试通过添加/删除引号、转义名称,但没有任何改变。不过谢谢你的帮助。
    • 您介意编辑您的答案以包含 doctrine:schema:update --dump-sql 的输出吗?在插入“ORM\Table”行之前/之后应该有所不同。
    • 不,输出完全一样,只是查询中有引号。
    猜你喜欢
    • 2017-10-19
    • 2016-09-04
    • 2014-07-31
    • 2013-05-21
    • 2020-06-27
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多