Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsigned primary keys #250

Closed
johnblythe opened this issue May 14, 2014 · 9 comments
Closed

Unsigned primary keys #250

johnblythe opened this issue May 14, 2014 · 9 comments

Comments

@johnblythe
Copy link

Is there a way to cause the autogen'd primary key ('id') to be unsigned?

@twoixter
Copy link
Contributor

I think not really, because the autogen'd column type is hardcoded right in the Db\Adapter::createTable method. However, what I'm doing is changing the column type after the main table is created.

I'm sure you've tried some of this, I post here my method if it is of some help.

Method 1) Preferred, since I think it will be compatible with all DB adapters. Right after creating the table, just modify the id column using a column class:

public function up()
{
    [... create the table ...]

    $col_id = new Phinx\Db\Table\Column();
    $col_id->setIdentity(true);  // Needed to be AUTO_INCREMENT
    $col_id->setType("integer");  // Or it can also be "biginteger"
    $col_id->setOptions(["signed" => false]);  // Makes the column unsigned
    $my_table->changeColumn("id", $col_id)->save();
}

Method 2) The obvious ALTER TABLE. Here the syntax is for MySQL only

public function up()
{
    [... create the table ...]

    $this->execute("ALTER TABLE `my_table` CHANGE `id` `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT");
}

@johnblythe
Copy link
Author

Thanks! I had ended up going with the second option, the ALTER route. I wish I could just use the the packaged methods, but I've found that I end up spending far too much time toying, testing, and tweaking as opposed to just running an execute() command and being done with it.

Thanks again!

@robmorgan
Copy link
Member

thx @twoixter . I'd recommend to use either of those methods for now.

@adayth
Copy link

adayth commented Jul 13, 2015

Alternative method overriding Phinx classes.
https://gist.github.com/adayth/f0ead7bd210e3968208e
Edit: wrong link

@paul-at-cybr
Copy link

This is a feature I miss. Doesn't necessarily break the app, but it hurts inside knowing that all my auto-increment id's will be signed for no practical reason.

@steefaan
Copy link

@robmorgan I would love to see this feature. Sure @twoixter solution works great (thanks!) but it would be nicer to have the possibility to configure it directly especially in such a nice library like yours :)

@caseycs
Copy link

caseycs commented Sep 30, 2015

Just posted a small update for @twoixter solution: https://gist.github.com/adayth/f0ead7bd210e3968208e#gistcomment-1585602

@bubach
Copy link

bubach commented Dec 16, 2019

Who came up with the brilliant idea to set primary keys as signed in the first place, that's one of the reasons I'm starting to think raw SQL files for migrations wasn't such a bad idea after all... Overall Phinx feeling -> This is just retarded.

@robmorgan
Copy link
Member

Hi @bubach that was me and it's why I no longer work on this project anymore. You might want to check out this instead: https://flywaydb.org/ 👍

rootpd pushed a commit to remp2020/crm-application-module that referenced this issue Jan 27, 2023
…igned

Due to 0.13 breaking change in Phinx library, type of primary key changed from
'int' to 'unsigned int'. This breaks our migrations because we always reference
primary keys in foreign keys as 'int'. Phinx provides backward compatible flag
to turn on the former behaviour (in 'feature-flags' settings)

see:
- https://book.cakephp.org/phinx/0/en/configuration.html#feature-flags
- cakephp/phinx#250
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants