1

我有这两个迁移文件,我想将“clients”的主键添加为“samples”中的外键,但是在迁移时,它给了我一个错误“外键约束的格式不正确”。

这是命令行的屏幕截图:

错误截图

这是我的代码:

创建客户

<?php
use Migrations\AbstractMigration;

class CreateClients extends AbstractMigration
{
    public function change()
    {
        $table = $this->table('clients');

        $table->addColumn('name', 'string', [
            'limit' => 100,
            'null' => false,
        ]);
        $table->addColumn('title', 'string', [
            'default' => null,
            'limit' => 100,
            'null' => true,
        ]);
        $table->addColumn('street', 'string', [
            'limit' => 255,
            'null' => false,
        ]);
        $table->addColumn('city', 'string', [
            'limit' => 255,
            'null' => false,
        ]);
        $table->addColumn('state', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => false,
        ]);
        $table->addColumn('zipcode', 'biginteger', [
            'limit' => 10,
            'null' => false,
        ]);
        $table->addColumn('country', 'string', [
            'limit' => 255,
            'null' => false,
        ]);
        $table->addColumn('phone', 'biginteger', [
            'limit' => 10,
            'null' => false,
        ]);
        $table->addColumn('emailprimary', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => true,
        ]);
        $table->addColumn('emailsecondary', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => true,
        ]);
        $table->addColumn('username', 'string', [
            'limit' => 255,
            'null' => false,

        ])->addIndex(array('username'), array('unique' => true));
        $table->addColumn('password', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => false,
        ]);
        $table->addColumn('billing_title', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => true,
        ]);
        $table->addColumn('billing_street', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => true,
        ]);
        $table->addColumn('billing_city', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => true,
        ]);
        $table->addColumn('billing_state', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => true,
        ]);
        $table->addColumn('billing_zipcode', 'biginteger', [
            'default' => null,
            'limit' => 20,
            'null' => true,
        ]);
        $table->addColumn('billing_country', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => true,
        ]);
        $table->addColumn('billing_phone', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => true,
        ]);
        $table->create();
    }
}

创建样本

<?php
use Migrations\AbstractMigration;

class CreateSamples extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-change-method
     * @return void
     */
    public function change()
    {
        $table = $this->table('samples');

        $table->addColumn('name', 'string', [
            'limit' => '100',
            'null' => false,
        ]);
        $table->addColumn('client_id', 'integer', [
            'null' => false,
        ]);
        $table->addForeignKey('client_id', 'clients', 'id', array('delete'=> 'SET_NULL', 'update'=> 'NO_ACTION'));
        $table->save();

        $table->create();
    }
}
4

1 回答 1

2

在您的 CreateSamples 迁移中,您将该client_id列定义为非空。但是,当您定义外键时,您将其作为on delete, set null. 那不能一起去。而不是SET_NULL, CASCADEorRESTRICT会是更好的选择,具体取决于您的应用程序的逻辑。(限制会阻止您删除带有样本的客户端。)您还可以将client_id列定义为'null' => 'true'. 因此,例如,试试这个:

$table->addForeignKey('client_id', 'clients', 'id', array('delete'=> 'CASCADE', 'update'=> 'NO_ACTION'));

当您删除客户端时,这将删除样本。

更多关于 MySQL 行为的解释

于 2016-02-20T22:38:49.723 回答