phinx icon indicating copy to clipboard operation
phinx copied to clipboard

Error adding foreign key using TablePrefixAdapter

Open mcapodacqua opened this issue 11 years ago • 1 comments

Trying to create a foreign key for a table using a prefix I get this error:

 [PDOException]                                                                                
  SQLSTATE[HY000]: General error: 1005 Can't create table 'phinxtest.test_table2' (errno: 150)
#phinx.yml
#...
environments:
    default_migration_table: phinxlog
    default_database: development
    development:
        adapter: mysql
        host: localhost
        name: phinxtest
        user: root
        pass: ''
        port: 3306
        charset: utf8
        table_prefix: 'test_'

I created this migrations for test:

<?php

use Phinx\Migration\AbstractMigration;

class Table1 extends AbstractMigration
{

    public function change()
    {
        $this->table('table1')
            ->addColumn('foo','string')
            ->create();
    }
}
<?php

use Phinx\Migration\AbstractMigration;

class Table2 extends AbstractMigration
{
    public function change()
    {
        $this->table('table2')
            ->addColumn('bar','string')
            ->addColumn('relation','integer')
            ->addForeignKey('relation', 'table1',['id'])
            ->create();
    }
}

I added some debugging in the execution and I get this:

START TRANSACTIONCREATE TABLE `test_table1` (`id` INT(11) NOT NULL AUTO_INCREMENT, `foo` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;COMMIT == 20141229143613 Table1: migrated 0.2644s

 == 20141229145502 Table2: migrating
START TRANSACTIONCREATE TABLE `test_table2` (`id` INT(11) NOT NULL AUTO_INCREMENT, `bar` VARCHAR(255) NOT NULL, `relation` INT(11) NOT NULL, PRIMARY KEY (`id`),  FOREIGN KEY (`relation`) REFERENCES `table1` (`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;

It looks like it's not adding the prefix while creating the foreign key.

Version:

Phinx by Rob Morgan - https://phinx.org. version 0.4.1

mcapodacqua avatar Dec 29 '14 15:12 mcapodacqua

Should be good to go, please reopen if you still experience issues!

shadowhand avatar Dec 30 '14 18:12 shadowhand