phinx icon indicating copy to clipboard operation
phinx copied to clipboard

Cannot change primary key to multi-column one with SQLite

Open InvisibleSmiley opened this issue 4 years ago • 7 comments

When using the SQLite adapter, tables can be created with a multi-column primary key.

Trying to change the primary key to a multi-column one afterwards fails (whether it previously was a single-column or multi-column one) with InvalidArgumentException "Invalid value for primary key: [...]".

InvisibleSmiley avatar Dec 16 '21 08:12 InvisibleSmiley

Could you provide an example migration?

MasterOdin avatar Dec 16 '21 16:12 MasterOdin

<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class Foo extends AbstractMigration
{
    public function change(): void
    {
        $this->create();
        $this->modify();
    }

    private function create(): void
    {
        $this->table('foo', ['id' => false, 'primary_key' => ['foo_id1', 'foo_id2']])
            ->addColumn('foo_id1', 'integer')
            ->addColumn('foo_id2', 'integer')
            ->create();
    }

    private function modify(): void
    {
        $this->table('foo')->addColumn('foo_bar', 'string')->save();
        $this->table('foo')->changePrimaryKey(['foo_bar', 'foo_id1', 'foo_id2'])->save();
    }
}

InvisibleSmiley avatar Dec 17 '21 10:12 InvisibleSmiley

I'm experiencing the same problem although in my case I'm trying to remove composite key and replace it with single column.

    public function change()
    {
        $this->table('orders')
            ->changePrimaryKey('id')
            ->changeColumn('id', 'integer', ['identity' => true])
            ->update();
    }

ArchangelDesign avatar May 18 '22 14:05 ArchangelDesign