1

I have added new column using migrations.

This is my new added field

$table->addColumn('lastname', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => false,
]);

This column added after modified field. I want to change column order and want to put it after column name. How can I do it using migrations ?

Niloy Rony
  • 602
  • 1
  • 8
  • 23

1 Answers1

1

You can use after in option, In adding time

$table->addColumn('firstname', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => false,
            'after' => 'name'
]);
Alimon Karim
  • 4,354
  • 10
  • 43
  • 68
  • 1
    You can't move where a column is. If you don't have any useful data in it yet, just roll back your migration, add the `after`, and run it again. If you have useful data, you'll need to add a new column where you want it, but with a different name, copy the data from the old column to the new, drop the old column, and rename the new one. Or do a `mysqldump` with `--complete-inserts`, edit the table create statement in the dump to rearrange the columns how you want, and re-import it. – Greg Schmidt Jul 14 '20 at 22:44
  • @GregSchmidt Actually, you can, via `changeColumn()` ;) – ndm Jul 15 '20 at 07:03
  • Well, damn. Cool. – Greg Schmidt Jul 15 '20 at 13:25
  • @ndm can we rename column name using changeColumn() ? – Niloy Rony Jul 18 '20 at 15:15
  • @NiloyRony Theoretically, yes, if you create a new column object (`\Phinx\Db\Action\ChangeColumn`) with a new name, and pass it as the second argument, but I'm not sure if that is auto-reversible, but it's much easier to simply use the [`renameColumn()`](https://book.cakephp.org/phinx/0/en/migrations.html#renaming-a-column) method anyways. – ndm Jul 18 '20 at 17:57
  • @ndm Before I have read https://book.cakephp.org/migrations/3/en/index.html this documentation and here no any advice for renameColumn, Thanks for ref. this documentation. – Niloy Rony Jul 18 '20 at 19:10
  • 1
    @NiloyRony Yeah, the migrations plugin docs do not cover all of Phinx's functionality, but there's this one tiny, easy to be overlooked sentence that tells you that and encourages you to read the Phinx docs ;) It probably wouldn't hurt if that were a more prominent note. – ndm Jul 18 '20 at 20:15