0

I'm trying to create a migration to add a foreign key to an existing column, it looks like this:

    use Migrations\AbstractMigration;

class AddForeignKeyIndexToPoints extends AbstractMigration
{
    public function up()
    {
        $this->table('points')
            ->addForeignKey('point_type_id', 'point_types', ['id'])
            ->save();
    }

    public function down() {

    }
}

which fails like this:

    == 20170202112138 AddPointTypeToPoints: migrating
Exception: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'point_type_id' in [C:\Users\Toby\Documents\projects\map\cake\vendor\robmorgan\phinx\src\Phinx\Db\Adapter\PdoAdapter.php, line 306]
2017-02-03 11:12:01 Error: [PDOException] SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'point_type_id'
Stack Trace:
#0 C:\Users\Toby\Documents\projects\map\cake\vendor\robmorgan\phinx\src\Phinx\Db\Adapter\PdoAdapter.php(306): PDO->exec('ALTER TABLE `po...')
#1 C:\Users\Toby\Documents\projects\map\cake\vendor\robmorgan\phinx\src\Phinx\Db\Adapter\MysqlAdapter.php(408): Phinx\Db\Adapter\PdoAdapter->execute('ALTER TABLE `po...')
#2 C:\Users\Toby\Documents\projects\map\cake\vendor\cakephp\migrations\src\CakeAdapter.php(480): Phinx\Db\Adapter\MysqlAdapter->addColumn(Object(Migrations\Table), Object(Phinx\Db\Table\Column))
#3 C:\Users\Toby\Documents\projects\map\cake\vendor\robmorgan\phinx\src\Phinx\Db\Table.php(625): Migrations\CakeAdapter->addColumn(Object(Migrations\Table), Object(Phinx\Db\Table\Column))
#4 C:\Users\Toby\Documents\projects\map\cake\vendor\cakephp\migrations\src\Table.php(81): Phinx\Db\Table->update()
#5 C:\Users\Toby\Documents\projects\map\cake\vendor\robmorgan\phinx\src\Phinx\Db\Table.php(662): Migrations\Table->update()
#6 C:\Users\Toby\Documents\projects\map\cake\config\Migrations\20170202112138_AddPointTypeToPoints.php(17): Phinx\Db\Table->save()
#7 C:\Users\Toby\Documents\projects\map\cake\vendor\robmorgan\phinx\src\Phinx\Migration\Manager\Environment.php(117): AddPointTypeToPoints->up()
#8 C:\Users\Toby\Documents\projects\map\cake\vendor\robmorgan\phinx\src\Phinx\Migration\Manager.php(309): Phinx\Migration\Manager\Environment->executeMigration(Object(AddPointTypeToPoints), 'up')
#9 C:\Users\Toby\Documents\projects\map\cake\vendor\robmorgan\phinx\src\Phinx\Migration\Manager.php(285): Phinx\Migration\Manager->executeMigration('default',Object(AddPointTypeToPoints), 'up')
#10 C:\Users\Toby\Documents\projects\map\cake\vendor\robmorgan\phinx\src\Phinx\Console\Command\Migrate.php(113): Phinx\Migration\Manager->migrate('default', NULL)
#11 C:\Users\Toby\Documents\projects\map\cake\vendor\cakephp\migrations\src\ConfigurationTrait.php(165): Phinx\Console\Command\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 C:\Users\Toby\Documents\projects\map\cake\vendor\cakephp\migrations\src\Command\Migrate.php(58): Migrations\Command\Migrate->parentExecute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 C:\Users\Toby\Documents\projects\map\cake\vendor\symfony\console\Command\Command.php(255): Migrations\Command\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#14 C:\Users\Toby\Documents\projects\map\cake\vendor\symfony\console\Application.php(829): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 C:\Users\Toby\Documents\projects\map\cake\vendor\symfony\console\Application.php(191): Symfony\Component\Console\Application->doRunCommand(Object(Migrations\Command\Migrate), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 C:\Users\Toby\Documents\projects\map\cake\vendor\symfony\console\Application.php(122): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 C:\Users\Toby\Documents\projects\map\cake\vendor\cakephp\migrations\src\Shell\MigrationsShell.php(98): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput))
#18 [internal function]: Migrations\Shell\MigrationsShell->main('migrations', 'migrate')
#19 C:\Users\Toby\Documents\projects\map\cake\vendor\cakephp\cakephp\src\Console\Shell.php(466): call_user_func_array(Array, Array)
#20 C:\Users\Toby\Documents\projects\map\cake\vendor\cakephp\migrations\src\Shell\MigrationsShell.php(130): Cake\Console\Shell->runCommand(Array, true, Array)
#21 C:\Users\Toby\Documents\projects\map\cake\vendor\cakephp\cakephp\src\Console\ShellDispatcher.php(227): Migrations\Shell\MigrationsShell->runCommand(Array,true, Array)
#22 C:\Users\Toby\Documents\projects\map\cake\vendor\cakephp\cakephp\src\Console\ShellDispatcher.php(182): Cake\Console\ShellDispatcher->_dispatch(Array)
#23 C:\Users\Toby\Documents\projects\map\cake\vendor\cakephp\cakephp\src\Console\ShellDispatcher.php(128): Cake\Console\ShellDispatcher->dispatch(Array)
#24 C:\Users\Toby\Documents\projects\map\cake\bin\cake.php(34): Cake\Console\ShellDispatcher::run(Array)
#25 {main}

There's at least one example in the Phinx docs that leads me to believe this should work, but I can't get it to - what am I doing wrong?

toby1kenobi
  • 1,609
  • 1
  • 14
  • 24
  • Works fine for me. If I were to guess, I'd say that the error maybe doesn't actually stem from the shown code. Whenever receiving errors, please always post **the _complete_ error**, that is, **including the _full_ stacktrace** (ideally copied from the logs where it is available in a properly readable fashion). – ndm Feb 02 '17 at 20:08
  • Sure thing, now added – toby1kenobi Feb 03 '17 at 11:16
  • Looking at the stacktrace, the problem stems from the the `AddPointTypeToPoints` migration, not from the `AddForeignKeyIndexToPoints` one. If you had already applied that migration, then you need to check why it's being applied again. That could for example happen when the `phinxlog` table is broken/missing. – ndm Feb 03 '17 at 14:46
  • Gah, you are right - thank you so much, apologies for not using my eyes – toby1kenobi Feb 06 '17 at 11:58

0 Answers0