簡體   English   中英

錯誤 1215 無法添加外鍵約束 symfony

[英]error 1215 cannot add foreign key constraint symfony

我在創建實體之間的關系時遇到了 symfony 問題。 我希望在我的身體和我的身體注冊大學之間建立一個多對一的關系。 銘文與大學有關。

班級銘文:

/**
* Inscription
*
* @ORM\Table(name="Inscription")
*     @ORM\Entity(repositoryClass="IEPA\PlatformBundle\Repository\InscriptionRepository")
*/
class Inscription
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=true)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
* 
* @ORM\ManyToOne(targetEntity="IEPA\PlatformBundle\Entity\Universites")
* @ORM\JoinColumn(nullable=false)
*/
private $univInsc;
}

類大學:

/**
* Universites
*
* @ORM\Table(name="universites")
* @ORM\Entity
* @ORM\Entity(repositoryClass="IEPA\PlatformBundle\Repository\UniversitesRepository")
*/
class Universites
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;
}

當我更新數據庫(php 應用程序/控制台原則:架構:更新 --force)時,出現以下錯誤:

[Doctrine\DBAL\Exception\DriverException]
An exception occured while executing `ALTER TABLE inscription ADD CONTRAINT FK_D80C7901C5E240F6 FOREIGN KEY (univ_insc_id) REFERENCE universites (id):
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

驗證您是否已經在您的銘文實體中插入了行。 如果是,請刪除他,或者如果您無法刪除它們,因為您的網站已上線。

像這樣改變這一行:

@ORM\JoinColumn(nullable=false)

@ORM\JoinColumn(nullable=true)

並將 inversedBy="inscriptions" 添加到 $univInsc 中,如下所示:

@ORM\ManyToOne(targetEntity="IEPA\PlatformBundle\Entity\Universites", inversedBy="inscriptions")

並嘗試再次執行學說:更新。

為此,您必須在兩個實體中聲明關系。 一個是多對一,另一個是一對多。 並說 Doctrine 反轉數據的方式:

    /**
    * Inscription
    *
    * @ORM\Table(name="Inscription")
    *     @ORM\Entity(repositoryClass="IEPA\PlatformBundle\Repository\InscriptionRepository")
    */
    class Inscription
    {
    /**
    * @ORM\Id
    * @ORM\Column(name="id", type="integer", nullable=true)
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;
    /**
    * @var integer
    * 
    * @ORM\ManyToOne(targetEntity="IEPA\PlatformBundle\Entity\Universites", inversedBy="inscriptions")
    * @ORM\JoinColumn(name="idInscription", referencedColumnName="idInscription")
    */
    private $univInsc;
    }

Au niveau de ton université, on aura tes inscriptions qui seront référencées :


在您的大學中,我們添加了一系列銘文:

    /**
    * Universites
    *
    * @ORM\Table(name="universites")
    * @ORM\Entity
    * @ORM\Entity(repositoryClass="IEPA\PlatformBundle\Repository\UniversitesRepository")
    */
    class Universites
    {
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

/**
 * @var Collection
 *
 * @ORM\OneToMany(targetEntity="IEPA\PlatformBundle\Entity\Inscription", mappedBy="univInsc")
 */
    private $inscriptions;
    }

只是一件小事:命名約定是用單數詞命名實體。 您可以使用復數,但我建議您不要在同一個項目中同時使用兩者。

您收到此錯誤是因為foreign key constraint已存在。

嘗試直接刪除事物擁有(多對一)方面的foreign key constraint 刪除后,重新運行您的學說 CLI 命令以執行 SQL 更改。 在 ZF2 中,這是php index.php orm:schema-tool:update --force命令,它將重新創建索引。

在為其他項目轉儲一些表后,我遇到了這個問題,問題是整理。 舊表在 utf8_unicode_ci 中,新表在 utf8mb4_unicode_ci 中,所以我在 dominic.yaml 中更改了它。

從:

    doctrine:
        dbal:
            # configure these for your database server
            driver: 'pdo_mysql'
            server_version: '5.7'
            charset: utf8mb4
            default_table_options:
                charset: utf8mb4
                collate: utf8mb4_unicode_ci

到:

    doctrine:
        dbal:
            # configure these for your database server
            driver: 'pdo_mysql'
            server_version: '5.7'
            charset: utf8
            default_table_options:
                charset: utf8
                collate: utf8_unicode_ci

和...正在更新數據庫架構...

 5 queries were executed

[OK] 數據庫架構更新成功!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM