0

我正在尝试使用 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

有任何想法吗?

4

1 回答 1

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-06-12T01:24:58.580 回答