如何使用Doctrine 1.1找出MySQL表的外键约束?

问题描述:

When two models are related to each other, Doctrine automatically generates foreign key constraints in the MySQL database beautifully. The default behaviour for onDelete and onUpdate RESTRICT, but can also be set to SET NULL or CASCADE.

Now, I want to create a migration diff from an existing database compared to a YAML file. To do this, I use the Doctrine::generateModelsFromDB() function. I use an overloaded versoin of Doctrine_Migration_Diff to do the diff work for me, that takes care of non-matching class names, MySQL field lengths etc. It does all stuff nicely, but there is a problem with these foreign key constraints.

When I create PHP classes from a MySQL database, it seems Doctrine doesn't set the 'onDelete' and 'onUpdate' to the appropriate values. When I then try to diff these PHP classes against classes that do have 'onDelete' and/or 'onUpdate' set, Doctrine tries to add a new foreign key that already exists, and MySQL fails.

I worked around this error temporarily by manually ignoring all changed foreign keys in my overloaded Doctrine_Migration_Diff, but this really is no solution.

What I want: if Doctrine thinks a new Foreign Key needs to be added, chance is it already exists, and what its onDelete and onUpdate behaviour are set to. How can I check that, if I have the local field name and the foreign field name?

P.S. If somebody is certain this was fixed in Doctrine 1.2 (which I doubt), please don't advise me to upgrade. I have specific reasons to work with 1.1 in this case, but that is a different story. I would rather be pointed into the direction of what code change provided this fix, that might be a step closer to a workaround.

当两个模型相互关联时,Doctrine会自动在MySQL数据库中自动生成外键约束。 onDelete和onUpdate RESTRICT的默认行为,但也可以设置为SET NULL或CASCADE。 p>

现在,我想从现有数据库创建一个迁移差异,与YAML文件进行比较。 为此,我使用 Doctrine :: generateModelsFromDB() code>函数。 我使用Doctrine_Migration_Diff的重载versoin为我做差异工作,它负责不匹配的类名,MySQL字段长度等。它很好地完成所有事情,但是这些外键约束存在问题。 p >

当我从MySQL数据库创建PHP类时,似乎Doctrine没有将'onDelete'和'onUpdate'设置为适当的值。 当我尝试将这些PHP类与具有'onDelete'和/或'onUpdate'集合的类进行区分时,Doctrine会尝试添加一个已存在的新外键,并且MySQL失败。 p>

我想要的是:如果Doctrine认为需要添加新的外键,则它可能已经存在,以及它的onDelete和onUpdate行为设置为什么。 如果我有本地字段名称和外来字段名称,我该如何检查? p>

P.S。 如果有人确定这是在Doctrine 1.2(我怀疑)中修复的,请不要建议我升级。 在这种情况下,我有特定的理由与1.1合作,但这是一个不同的故事。 我宁愿指出提供此修复程序的代码更改的方向,这可能是更接近解决方法的一步。 p> div>

To answer my own question: I looked into the Doctrine source code for quite some time, and found out that the MySQL importer does not have functionality to do this.

In other words, it is impossible without extending Doctrine yourself.

Well Ive never had this issue. But i generally use Doctrine with Symfony and they may have customized the migration tools beyond hooking them in to the Symfony CLI interface.

If i were you i would generate a new schmea from the DB, then generate the migration from the updated schema (compares schema to models i think). Then regnerate my models afterwards.

However, Id reccommend making the schema authoritative. If you have to use a legacy DB then generate your schema from the DB when you start. Then all db changes after that, make to the schema and then migrate using schmea/models.