Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Foreign Key using Phinx

Tags:

php

mysql

phinx

I know there has been a couple of posts similar to this one, but they are not giving me the answer I need. So, I'm using Phinx and I am trying to add a foreign key, but I get the following error:

General error: 1215 Cannot add foreign key constraint

Here is the php file with the foreign keys:

<?php

use Phinx\Migration\AbstractMigration;

class CustomQuotes extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('custom_quotes', ['id' => false, 'primary_key' => ['custom_quote_id']]);
        $table->addColumn('custom_quote_id', 'biginteger', ['identity' =>true, 'signed' => false])
        ->addColumn('account_id', 'biginteger', ['limit' => 20, 'null' => true])
        ->addColumn('quote_type_id', 'integer', ['limit' => 11, 'null' => true])
        ->addColumn('quote', 'string', ['limit' => 500, 'null' => true])
        ->addColumn('selected', 'integer', ['limit' => 4, 'null' => true, 'default' => 1])
        ->addForeignKey('account_id', 'accounts', 'account_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
        ->addForeignKey('quote_type_id', 'quote_types', 'quote_type_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
        ->create();
    }
}

The two tables I am trying to get the id from are created before the table above. Here are the other two tables:

Quote types table

use Phinx\Migration\AbstractMigration;

class CustomQuotes extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('custom_quotes', ['id' => false, 'primary_key' => ['custom_quote_id']]);
        $table->addColumn('custom_quote_id', 'biginteger', ['identity' =>true, 'signed' => false])
        ->addColumn('account_id', 'biginteger', ['limit' => 20, 'null' => true])
        ->addColumn('quote_type_id', 'integer', ['limit' => 11, 'null' => true])
        ->addColumn('quote', 'string', ['limit' => 500, 'null' => true])
        ->addColumn('selected', 'integer', ['limit' => 4, 'null' => true, 'default' => 1])
        ->addForeignKey('account_id', 'accounts', 'account_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
        ->addForeignKey('quote_type_id', 'quote_types', 'quote_type_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
        ->create();
    }
}

Accounts table

<?php

use Phinx\Migration\AbstractMigration;

class MakeAccountsTable extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('accounts', ['id' => false, 'primary_key' => ['account_id']]);
        $table->addColumn('account_id', 'biginteger', ['identity' =>true, 'signed' => false])
        ->addColumn('css_user_guid', 'string', ['limit' => 40])
        ->addColumn('state_id', 'integer', ['limit' => 11, 'null' => true])
        ->addColumn('phone', 'string', ['limit' => 45, 'null' => true])
        ->addColumn('street', 'string', ['limit' => 45, 'null' => true])
        ->addColumn('city', 'string', ['limit' => 45, 'null' => true])
        ->addColumn('zip', 'string', ['limit' => 45, 'null' => true])
        ->addColumn('state', 'string', ['limit' => 45, 'null' => true])
        ->addColumn('terms_of_service_accepted', 'boolean', ['limit' => 1, 'null' => true, 'default' => 0])
        ->addColumn('quote', 'string', ['limit' => 300, 'null' => true])
        ->addColumn('date_created', 'datetime', ['null' => true])
        ->create();
    }
}
like image 501
hjcoder18 Avatar asked Mar 11 '23 01:03

hjcoder18


1 Answers

So I figured it out. Turns out that I had to make my account_id and quote_type_id unsigned in the custom_quotes table because I made them unsigned in the the accounts and quote_type tables. It makes sense now. Here is the solution:

<?php

use Phinx\Migration\AbstractMigration;

class CustomQuotes extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('custom_quotes', ['id' => false, 'primary_key' => ['custom_quote_id']]);
        $table->addColumn('custom_quote_id', 'biginteger', ['identity' =>true, 'signed' => false])
        ->addColumn('account_id', 'biginteger', ['limit' => 20, 'null' => true, "signed" => false])
        ->addColumn('quote_type_id', 'integer', ['limit' => 11, 'null' => true, "signed" => false])
        ->addColumn('quote', 'string', ['limit' => 500, 'null' => true])
        ->addColumn('selected', 'integer', ['limit' => 4, 'null' => true, 'default' => 1])
        ->addForeignKey('account_id', 'accounts', 'account_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
        ->addForeignKey('quote_type_id', 'quote_types', 'quote_type_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
        ->create();
    }
}
like image 115
hjcoder18 Avatar answered Mar 19 '23 10:03

hjcoder18