简体   繁体   English

Doctrine不会生成跨数据库外键

[英]Doctrine doesn't generate cross-database foreign keys

I have a bunch of models setup in Doctrine, where some of the models are in different databases. 我在Doctrine中设置了一些模型,其中一些模型位于不同的数据库中。 Doctrine's schema generation tool seems to be generating inter-database foreign keys, but not cross-database foreign keys. Doctrine的模式生成工具似乎是生成数据库间外键,而不是跨数据库外键。

For example: 例如:

/**
 * @ORM\Entity
 * @ORM\Table(name="db1.Contact")
 **/
class Contact {
    /** 
     * @ORM\Id 
     * @ORM\Column(type="integer") 
     * @ORM\GeneratedValue 
     **/
    private $id;
}

/**
 * @ORM\Entity
 * @ORM\Table(name="db2.Subscription")
 **/
class Subscription {
    /** 
     * @ORM\Id 
     * @ORM\Column(type="integer") 
     * @ORM\GeneratedValue 
     **/
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Contact")
     * @ORM\JoinColumn(name="contact_id", referencedColumnName="id")
     */
    private $contact;
}

Critically, hydrating these entities works totally fine , but the schema tool simply doesn't generate the foreign keys. 重要的是, 保湿这些实体的工作完全正常 ,但架构工具根本不会生成外键。

Has anyone ran into this before? 有没有人遇到过这个? There is another SO post however it is unfortunately unanswered. 还有另一个SO帖子,但不幸的是没有答案。

Doctrine does not support cross-database foreign keys, however it can be modified to do so. Doctrine不支持跨数据库外键,但可以对其进行修改。 The library seems to take a "not everyone can support it, so no one should" approach. 图书馆似乎采取了“不是每个人都可以支持它,所以没有人应该”接近。 This instance works for MySQL. 这个实例适用于MySQL。

When generating a schema, a pre-step is ran using the RemoveNamespacedAssets visitor. 生成模式时,使用RemoveNamespacedAssets访问者运行预步骤。 This removes all references to any classes outside of what you are generating. 这将删除对您生成的内容之外的任何类的所有引用。

In the acceptForeignKey function of that class, comment the following code: acceptForeignKey函数中,注释以下代码:

// The table may already be deleted in a previous
// RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that
// point to nowhere.
if ( ! $this->schema->hasTable($fkConstraint->getForeignTableName())) {
    $localTable->removeForeignKey($fkConstraint->getName());
    return;
}

Running a schema creation or update will now create foreign keys as expected. 现在,运行模式创建或更新将按预期创建外键。 It is possible this will have other unintended side effects but I haven't ran into any yet. 这可能会产生其他意想不到的副作用,但我还没有遇到任何副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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