CakePHP の migrations で外部キーを作る時は、unsigned にするといいかもしれない話

ある長年動いていた CakePHP 3 アプリを移行するために、一旦 CakePHP5 の tuta をやり始めたところだ。

Cake公式のチュートリアルはブログアプリをつくることになるのだが、DB の作成はベタで SQLを叩くようになっている。そのままやってももちろんいいし最短時間で目的の状態になるのだが、今回は移行時に使うことになるのであえて migrations を使ってみた。

で要点から、 migrations はデフォルトで、id フィールドを作ってくれるのですがそれが int(11) になっているようです。一方、migrations で integer でフィールドを作成すると int(10) になるのがデフォルトのようです。
となると結果として外部キーを作成するときにフィールドの桁数が合わずに作れずエラーになるという事象に遭遇したという話です。

ブログチュータで実際のコードはこんな感じになります。

ファイル名の先頭部分は日時を示します。 空のファイル自身もこんなふうに migrations で生成できます。
./app/bin/cake migrations create CreateUsers

でユーザーテーブルはこれです。

<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class CreateUsers extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     * @return void
     */
    public function change(): void
    {
        $table = $this->table('users');
        $table->addColumn('email', 'string', [
            'default' => '',
            'limit' => 255,
            'null' => false,
            'comment' => ''
        ])
        ->addIndex(['email']);
        $table->addColumn('password', 'string', [
            'default' => '',
            'limit' => 255,
            'null' => false,
        ]);
        $table->addColumn('created', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->addColumn('modified', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->create();
    }
}

アーティクルテーブルはこうなりました。

user_id フィールドの signed を false にする必要がある。

<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class CreateArticles extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     * @return void
     */
    public function change(): void
    {
        $table = $this->table('articles');
        $table->addColumn('user_id', 'integer', [
            'null' => false,
            'signed' => false,
        ]);
        // ->addIndex(['user_id']);
        $table->addColumn('title', 'string', [
            'default' => '',
            'limit' => 255,
            'null' => false,
        ]);
        $table->addColumn('slug', 'string', [
            'default' => '',
            'limit' => 191,
            'null' => false,
        ]);
        $table->addColumn('body', 'text', [
            'default' => null,
            'null' => true,
        ]);
        $table->addColumn('published', 'boolean', [
            'default' => false,
        ]);
        $table->addColumn('created', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->addColumn('modified', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->addIndex('slug', [
            'unique' => true
        ]);
        $table->addForeignKey('user_id', 'users', 'id',
            [
                'delete' => 'NO_ACTION', 
                'update' => 'NO_ACTION'
            ]
        );
        // $table->changePrimaryKey(['article_id', 'tag_id']);
        $table->create();
    }
}

tags テーブルはこうなる。

<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class CreateTags extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     * @return void
     */
    public function change(): void
    {
        $table = $this->table('tags');
        $table->addColumn('title', 'string', [
            'limit' => 191,
        ]);
        $table->addColumn('created', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->addColumn('modified', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->addIndex('title', [
            'unique' => true
        ]);
        $table->create();
    }
}

Articles テーブルと同じように外部キーを保存するフィールドはアンサインドにする。

<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class CreateAticlesTags extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     * @return void
     */
    public function change(): void
    {
        $table = $this->table('aticles_tags');
        $table->addColumn('article_id', 'integer', [
            'null' => false,
            'signed' => false,
        ]);
        $table->addColumn('tag_id', 'integer', [
            'null' => false,
            'signed' => false,
        ]);
        $table->addColumn('created', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->addColumn('modified', 'datetime', [
            'default' => null,
            'null' => false,
        ]);
        $table->addForeignKey('tag_id', 'tags', 'id', [
            'delete' => 'NO_ACTION', 'update' => 'NO_ACTION'
        ]);
        $table->addForeignKey('article_id', 'articles', 'id', [
            'delete' => 'NO_ACTION', 'update' => 'NO_ACTION'
        ]);
        $table->create();
    }
}

これで DBの構築はできたが、先に進んだ所で間違いがあれば修正していくかもしれない。