【问题标题】:Cannot add foreign key constraint using phinx无法使用 phinx 添加外键约束
【发布时间】:2020-06-11 22:00:04
【问题描述】:

我正在尝试使用 phinx 添加几个相关的表。我创建了我的迁移:

<?php

use Phinx\Migration\AbstractMigration;

class CreatesCognitiveMappingTables extends AbstractMigration
{
    public function up()
    {
        $this->table("cognitive_mapping")
            ->changeColumn('id', 'biginteger', ['identity' => true])
            ->addColumn("user_id", "biginteger", ["signed" => false])
            ->addColumn("participant_id", "integer")
            ->addColumn("session_number", "integer")
            ->addColumn("date_time", "datetime")
            ->addColumn("image_file_link", "text")
            ->addColumn("time", "decimal", ["precision" => 10, "scale" => 5])
            ->addColumn("test_trial_complete", "boolean")
            ->addTimestamps()
            ->create();

        $child = $this->table("cognitive_mapping_dragdrop_results");
        $child
            ->changeColumn('id', 'biginteger', ['identity' => true])
            ->addColumn("cognitive_mapping_id", "biginteger", ["signed" => false])
            ->addColumn("box_number", "integer")
            ->addColumn("correct_answer", "text")
            ->addColumn("given_answer", "text")
            ->addColumn("accuracy", "boolean")
            ->addTimestamps()
            ->create();

        $child
            ->addForeignKey("cognitive_mapping_id", "cognitive_mapping", "id", ["delete" => "CASCADE"])
            ->save();
    }

    public function down()
    {
        $this->table("cognitive_mapping_dragdrop_results")->drop()->save();
        $this->table("cognitive_mapping")->drop()->save();
    }
}

但是当我运行迁移时,我收到一条错误消息,说它无法添加外键约束:

我尝试了各种不同的排列方式,包括在不同的迁移文件中创建子表、删除级联、显式取消对父表中的主键的签名。所有这些都给了我同样的错误。有些东西我没看到。

我使用的是 php 7.3 和 phinx 版本 0.12.1

有什么想法吗?

【问题讨论】:

    标签: php mysql migration phinx


    【解决方案1】:

    啊,刚刚想通了。

    即使没有添加外键约束,表仍然被创建,所以我能够查看表结构:

    我注意到父表中的主键没有更改为无符号大整数,即使它的迁移链没有失败。

    我怀疑因此当我尝试在子表上添加约束时,字段不匹配(无法保存相同的数据)所以它失败了。

    我还怀疑该字段没有被更改,因为我试图在实际创建之前更改列,所以我重构了我的迁移以分解步骤:

    class CreatesCognitiveMappingTables extends AbstractMigration
    {
        public function up()
        {
            // * set up the bulk of the parent table
            $parent = $this->table("cognitive_mapping")
                ->addColumn("user_id", "biginteger", ["signed" => false])
                ->addColumn("participant_id", "integer")
                ->addColumn("session_number", "integer")
                ->addColumn("date_time", "datetime")
                ->addColumn("image_file_link", "text")
                ->addColumn("time", "decimal", ["precision" => 10, "scale" => 5])
                ->addColumn("test_trial_complete", "boolean")
                ->addTimestamps();
    
            // * create the table
            $parent->create();
    
            // * now that the table is created, update it to convert the primary key to a unsigned big integer
            $parent
                ->changeColumn('id', 'biginteger', ['identity' => true, 'signed' => false])
                ->save();
    
            // * Make the child table
            $child = $this->table("cognitive_mapping_dragdrop_results");
            $child
                ->changeColumn('id', 'biginteger', ['identity' => true])
                ->addColumn("cognitive_mapping_id", "biginteger", ["signed" => false]) // ! note that our to-be foreign key field is already an unsigned big int
                ->addColumn("box_number", "integer")
                ->addColumn("correct_answer", "text")
                ->addColumn("given_answer", "text")
                ->addColumn("accuracy", "boolean")
                ->addTimestamps()
                ->create();
    
            // * Now that both our foreign key and primary key fields match we can add the constraint
            $child
                ->changeColumn('id', 'biginteger', ['identity' => true, "signed" => false])
                ->addForeignKey("cognitive_mapping_id", "cognitive_mapping", "id", ["delete" => "CASCADE"])
                ->save();
        }
    
        public function down()
        {
            $this->table("cognitive_mapping_dragdrop_results")->drop()->save();
            $this->table("cognitive_mapping")->drop()->save();
        }
    }
    
    

    我能够成功运行迁移:

    并且约束添加正确:

    太好了:)

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 2020-09-12
      • 2017-07-16
      • 2013-03-10
      • 2021-11-05
      • 2018-02-13
      • 2018-05-16
      相关资源
      最近更新 更多