Error: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint in Phalcon
Error: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint in Phalcon
我正在尝试 运行 使用 Phalcon (Devtools 2.0.10) 进行迁移,但它一直抱怨 ERROR: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
上周我已经 运行 进行了迁移,它们都运行良好,但不确定现在有什么不同。我已经废弃数据库,重新安装 mysql,重新创建数据库,仍然做同样的事情。
问题似乎与一般的外键有关,而不是专门与这些外键有关。它从抱怨特定模型开始,所以我删除了 references
语句只是为了看看,它在下一个外键声明等处停止。引擎设置为 InnoDB,键的类型匹配并且它 运行 在我同事的机器上很好,所以我确定这不是语法问题,而是关于 SQL (server)
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
)
),
...
问题出在 SQL 级别的外键,而不是语法或 DDL 本身的问题。 Phalcon 似乎会在 /migrations
中按字母文件顺序创建 tables,并且不会考虑 table 结构。
正在执行
mysql> SET GLOBAL FOREIGN_KEY_CHECKS=0;
在 运行 迁移成功之前,当然是在将其设置回去之前。
我不确定在这种情况下是否有必要,但我删除了所有 mysql rpms,在重新开始之前删除了 /var/lib/mysql
目录。
我正在尝试 运行 使用 Phalcon (Devtools 2.0.10) 进行迁移,但它一直抱怨 ERROR: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
上周我已经 运行 进行了迁移,它们都运行良好,但不确定现在有什么不同。我已经废弃数据库,重新安装 mysql,重新创建数据库,仍然做同样的事情。
问题似乎与一般的外键有关,而不是专门与这些外键有关。它从抱怨特定模型开始,所以我删除了 references
语句只是为了看看,它在下一个外键声明等处停止。引擎设置为 InnoDB,键的类型匹配并且它 运行 在我同事的机器上很好,所以我确定这不是语法问题,而是关于 SQL (server)
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
)
),
...
问题出在 SQL 级别的外键,而不是语法或 DDL 本身的问题。 Phalcon 似乎会在 /migrations
中按字母文件顺序创建 tables,并且不会考虑 table 结构。
正在执行
mysql> SET GLOBAL FOREIGN_KEY_CHECKS=0;
在 运行 迁移成功之前,当然是在将其设置回去之前。
我不确定在这种情况下是否有必要,但我删除了所有 mysql rpms,在重新开始之前删除了 /var/lib/mysql
目录。