我正在尝试使用Phalcon进行迁移(Devtools 2.0.10),但它一直在抱怨ERROR: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
我上周进行了迁移,他们都运行良好,不确定现在有什么不同。我已经废弃了数据库,重新安装了mysql,重新创建了数据库,仍然做同样的事情。
这个问题似乎与外键有关,而不是具体的。它首先抱怨一个特定的模型,所以我删除了references
语句只是为了看,它在下一个外键声明处停止,依此类推。引擎设置为InnoDB,键的类型匹配,它在我的同事的机器上运行正常,所以我确定它不是语法问题,而是SQL(服务器)的特定内容
class ModuleTranslationsMigration_100 extends Migration
{
public function morph()
{
$this->morphTable('', array(
...
'references' => array(
new Reference(
'module_translations_ibfk_1',
array(
'referencedSchema' => 'learning',
'referencedTable' => 'modules',
'columns' => array('module_id'),
'referencedColumns' => array('id')
)
),
new Reference(
'module_translations_ibfk_2',
array(
'referencedSchema' => 'learning',
'referencedTable' => 'languages',
'columns' => array('language_id'),
'referencedColumns' => array('id')
)
)
),
...
class LanguagesMigration_100 extends Migration
{
public function morph()
{
$this->morphTable('languages', array(
'columns' => array(
new Column(
'id',
array(
'type' => Column::TYPE_CHAR,
'notNull' => true,
'size' => 2,
'first' => true
)
),
...
class ModulesMigration_100 extends Migration
{
public function morph()
{
$this->morphTable('modules', array(
'columns' => array(
new Column(
'id',
array(
'type' => Column::TYPE_INTEGER,
'unsigned' => true,
'notNull' => true,
'autoIncrement' => true,
'size' => 10,
'first' => true
)
),
...
答案 0 :(得分:0)
问题是SQL级别的外键,而不是语法或DDL本身的问题。似乎Phalcon将在/migrations
中按字母顺序创建表格,并且不考虑表格结构。
执行
mysql> SET GLOBAL FOREIGN_KEY_CHECKS=0;
在运行迁移之前完成了这一操作,然后再将其设置回来。
我不确定在这种情况下是否有必要但我删除了所有mysql rpms,在重新开始之前删除了/var/lib/mysql
目录。