我有这两个迁移文件,我想将“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();
}
}