Reputation: 5502
I am new to Cakephp and building an application with Cakephp3.3, I am working on Migrations, I have to create a user_infos
table, and want to add a new column user_id
, I am able to add new columns through migrations but I don't know how to add foreign key.
here is my migration file
public function change()
{
$table = $this->table('user_infos');
$table->addColumn('user_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
]);
$table->addColumn('title', 'string', [
'default' => null,
'limit' => 255,
'null' => false,
]);
$table->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
]);
$table->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
]);
$table->create();
}
Upvotes: 2
Views: 4663
Reputation: 54
In Phinx, you can use the below code to add foreign key with cascading
$table->addForeignKey('user_id', 'users', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE']);
Upvotes: 3
Reputation: 824
Your migration seems to be based on Phinx
; you can find relevant methods in Phinx\Db\Table
. Add
->addIndex(['user_id'])
->addForeignKey('user_id', 'users', 'id')
to your migration for the constraint on the users
table.
Upvotes: 7