我有这两个迁移文件,我想在" samples"中添加" clients" 的主键作为外键。 但是在迁移时,它会给我一个错误,"外键约束形成错误"。
以下是命令行的屏幕截图:
这是我的代码:
CreateClients
<?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();
}
}
CreateSamples
<?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();
}
}
答案 0 :(得分:1)
在CreateSamples迁移中,您将client_id
列定义为非null。但是,在定义外键时,可以将其设置为on delete, set null
。那不能一起去。取决于您的应用程序的逻辑,而不是SET_NULL
,CASCADE
或RESTRICT
将是更好的选择。 (限制会阻止您删除带有示例的客户。)您还可以将client_id
列定义为'null' => 'true'
。
所以,例如,试试这个:
$table->addForeignKey('client_id', 'clients', 'id', array('delete'=> 'CASCADE', 'update'=> 'NO_ACTION'));
删除客户端时会删除样本。
关于MySQL行为here的更多解释。