首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Phinx添加外键

使用Phinx添加外键
EN

Stack Overflow用户
提问于 2017-01-25 06:27:12
回答 1查看 5.1K关注 0票数 4

我知道已经有几个类似的帖子,但他们没有给我我需要的答案。所以,我正在使用Phinx,并试图添加一个外键,但我得到了以下错误:

代码语言:javascript
复制
General error: 1215 Cannot add foreign key constraint

下面是包含外键的php文件:

代码语言:javascript
复制
<?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();
    }
}

我尝试从中获取id的两个表是在上面的表之前创建的。下面是另外两个表:

报价类型表

代码语言:javascript
复制
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();
    }
}

帐户表

代码语言:javascript
复制
<?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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-25 06:38:37

所以我想通了。原来,我必须在custom_quotes表中使我的account_id和quote_type_id未签名,因为我在accounts和quote_type表中使它们未签名。现在说得通了。以下是解决方案:

代码语言:javascript
复制
<?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();
    }
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41840118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档