Doctrine does not create external foreign key constraints in MySQL

I have two tables, db1.Contact and db2.Recipient. Each recipient must be a contact, so I have a foreign key set between the two tables in the db1.Contact.ContactID field.

I present this in Recipient.php with the following annotation:

/**
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="\db1\Contact")
 * @ORM\JoinColumn(name="ContactID", referencedColumnName="ContactID")
 **/
private $Contact;

      

I don't need any code in Contact.php for this.

When I create the database (using the doctrine orm: schema-tool: create --dump-sql) I see that the Recipient.ContactID field is created and an index is assigned. However, no foreign key constraint is generated or queried to create. So to be clear, I don't get any errors, but the constraint is never generated.

How can I solve this? Surely the doctrine supports cross-site foreign keys? I checked the annotation documentation and looked to see if there are any options for the authoring tool, but I don't see an option to enable this feature.

+2


source to share


3 answers


I found a workaround for this, explain here . Basically, Doctrine uses cross-database foreign keys by default because not all database systems support it, but you can disable it by commenting out some code in the Doctrine library.



+1


source


Your association ManyToOne

seems to be wrong. You should use the FQCN of the entities if your codebase is in a namespace (or just the class name if the entities on both sides are in the same directory), not the table names .

for example



/**
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="YourNamespace\Entities\Contact")
 * @ORM\JoinColumn(name="ContactID", referencedColumnName="ContactID")
 **/
private $contact;

      

You can read more about mapping associations here .

0


source


Doctrine does not support Cross-DB FK:

In Doctrine, combining data across databases is not technically "supported" with a specially designed function, but you can make it work by cheating Doctrine a little.

Source: http://www.doctrine-project.org/2009/06/19/cross-database-joins.html

0


source







All Articles