0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CakePHP4_migrationでカラム位置を変更したい

Last updated at Posted at 2021-08-01

まえがき

CakePHPのmigrationを利用してカラム位置の修正を行う手順を記載します。

バージョン

CakePHP4

手順

・Usersテーブルをmigrationで作成
bin\cake bake migration CreateUsers login_id:string
bin\cake migrations migrate

・Usersテーブル情報修正用のmigrationファイル生成
bin\cake bake migration ChangeUsers

20210801024646_ChangeUsers.php
$table = $this->table('users');
$table->addColumn('loginId', 'string', [
    'after' => 'id'
]);
$table->update();

※上記のmigrationファイルでmigrate実行するとエラーが発生
bin\cake migrations migrate

PDOException: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'loginId' in C:\xampp\htdocs\cakephp_apps\todo\vendor\robmorgan\phinx\src\Phinx\Db\Adapter\PdoAdapter.php:194
20210801024646_ChangeUsers.php
$table = $this->table('users');
// addColumn -> changeColumnに変更
$table->changeColumn('loginId', 'string', [
    'after' => 'id'
]);
$table->update();

bin\cake migrations migrate

今度は正しく反映されました!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?