phinx icon indicating copy to clipboard operation
phinx copied to clipboard

How to make primary key unsigned in an existing table?

Open Zorinik opened this issue 3 years ago • 4 comments

Good morning, since as of phinx 0.13 PKs are unsigned by default, I would like to write a migration to update existing table and make their PKs unsigned, in order to preserve consistency between old and new future tables. I can't find a solution for doing this in the docs, there is a way? Thanks

Zorinik avatar Nov 03 '22 12:11 Zorinik

https://github.com/cakephp/phinx/pull/2159 will make it possible to restore previous behavior for now

We should also add docs on how to overcome those upgrading challenges however I agree, that we need some info on that.

dereuromark avatar Dec 22 '22 16:12 dereuromark

You'd want to use the changeColumn method . Assuming you're trying to change the default primary key that's created:

->changeColumn('id', 'integer', ['identity' => true, 'signed' => true])

Before:

mysql> SHOW COLUMNS FROM foo;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int unsigned | NO   | PRI | NULL    | auto_increment |
+-------+--------------+------+-----+---------+----------------+
1 rows in set (0.02 sec)

After:

mysql> SHOW COLUMNS FROM foo;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int          | NO   | PRI | NULL    | auto_increment |
+-------+--------------+------+-----+---------+----------------+
1 rows in set (0.01 sec)

@dereuromark Do you have a set place in mind to put this info? Just in the changelog for the next release that has feature flags?

MasterOdin avatar Jan 06 '23 16:01 MasterOdin

We might also want to add the details in docs and from release notes more like link to it.

dereuromark avatar Jan 06 '23 16:01 dereuromark

I guess add a note to it in the link I posted for this? I don't know another place that'd really fit.

MasterOdin avatar Jan 06 '23 16:01 MasterOdin