dongxiqian2787 2018-09-26 19:22
浏览 45

Cakephp迁移 - 常规错误:1215无法添加外键约束

I am having issues creating a foreign key in some migrations.

I have the following:

20180926184217_Courses.php

public function change()
{
    $table = $this->table('courses', ['id' => true, 'primary_key' => ['id']]);
    $table
        ->addColumn('name', 'string', ['default' => null, 'limit' => 150, 'null' => false])
        ->addColumn('town', 'string', ['default' => null, 'limit' => 50, 'null' => true])
        ->create();
}

20180926191546_Scorecards.php

public function change()
{
    $table = $this->table('scorecards', ['id' => true, 'primary_key' => ['id']]);
    $table
        ->addColumn('course_id', 'integer', ['default' => null, 'limit' => 10, 'null' => false])
        ->addColumn('description', 'string', ['default' => null, 'limit' => 255, 'null' => true])
        ->addColumn('tee', 'string', ['default' => null, 'limit' => 15, 'null' => false])
        ->addForeignKey('course_id', 'courses', 'id', ['delete' => 'SET_NULL', 'update' => 'NO_ACTION', 'constraint' => 'fk_scorecard_course'])
        ->create();
}

When I run bin/cake migrations migrate I get the following error:

Exception: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint in [*/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php, line 167] 2018-09-26 19:12:50 Error: [PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint in */vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php on line 167

Can anyone help please? I have looked at other answers on here and none seemed to help.

Dave

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器