Phinx: When adding foreign keys the table_prefix needs to be explicitly specified

Created on 5 Feb 2015  路  4Comments  路  Source: cakephp/phinx

Hey! Maybe this has already come up but I didn't find it. If it has, just close this, thank you!

When I add a foreign key to a table, the second parameter is the table name for the relation. It has to be specified including the table_prefix from phinx.yml to work.

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

My phinx.yml

paths:
    migrations: %%PHINX_CONFIG_DIR%%/migrations

environments:
    default_migration_table: phinxlog
    default_database: development
    development:
        adapter: mysql
        host: localhost
        name: lorem_ipsum
        user: root
        pass: ''
        port: 3306
        charset: utf8
        table_prefix: asd_

Migration for categories:

<?php

use Phinx\Migration\AbstractMigration;

class CreateCategories extends AbstractMigration
{
  /**
   * Migrate Up.
   */
  public function up()
  {
    $table = $this->table('categories');
    $table->addColumn('title', 'string')
          ->save();
  }

  /**
   * Migrate Down.
   */
  public function down()
  {
    $this->table('categories')->drop();
  }
}

Migration for products:

<?php

use Phinx\Migration\AbstractMigration;

class CreateProducts extends AbstractMigration
{
  /**
   * Migrate Up.
   */
  public function up()
  {
    $table = $this->table('products');
    $table->addColumn('category_id', 'integer')
          // not working (reports General error: 1215 Cannot add foreign key constraint)
          ->addForeignKey('category_id', 'categories', 'id')
          // working when adding the table_prefix 
          ->addForeignKey('category_id', 'asd_categories', 'id')
          ->save();
  }

  /**
   * Migrate Down.
   */
  public function down()
  {
    $this->table('products')->drop();
  }
}
bug

Most helpful comment

This bug is still in v0.7.2

All 4 comments

This should have been fixed by babe4927c0d59ae73417f82323da19fb93b75b29 in v0.4.2.

This bug is still present in v0.6.5

This bug is still in v0.7.2

This bug is still in v0.9.2

Was this page helpful?
0 / 5 - 0 ratings