Reputation:
I have created a migration using migrate create
and put following codes on it:
<?php
class m131220_121449_create_all_tables extends CDbMigration
{
// Use safeUp/safeDown to do migration with transaction
public function safeUp() {
// MEMBERS TABLE
$this->createTable("members", array(
'uniq_id' => 'pk',
'personel_num' => 'int(10) NOT NULL',
'password' => 'string NOT NULL',
'name' => 'string DEFAULT NULL',
'lastupdate' => 'timestamp DEFAULT CURRENT_TIMESTAMP',
), 'ENGINE=InnoDB');
// RESERVED TABLE
$this->createTable("reserved", array(
'uniq_id' => 'pk',
'personel_num' => 'int(10) NOT NULL',
'RsvdDay' => 'date NOT NULL',
'date_created' => 'timestamp DEFAULT CURRENT_TIMESTAMP',
), 'ENGINE=InnoDB');
// Add Foreign Keys Relations for RESERVED
$this->addForeignKey("fk_rsvd_user", "reserved", "personel_num", "members", "personel_num", "CASCADE", "RESTRICT");
}
}
But when I want to up this migration I got General Error #1005 when it tries to create Foreign Keys.
Here is the image of error:
The answer was found:
The reference of a foreign key has to be a Primary Key in other table.
Upvotes: 3
Views: 2324
Reputation: 567
$this->createTable("members", array(
'uniq_id' => 'pk',
'personel_num' => 'int(10) NOT NULL',
'password' => 'string NOT NULL',
'name' => 'string DEFAULT NULL',
'lastupdate' => 'timestamp DEFAULT CURRENT_TIMESTAMP',
'UNIQUE KEY `personel_num` (`personel_num`)',
), 'ENGINE=InnoDB');
Make personel_num unique to make it a foreign key. You could also just remove uniq_id and make personel_num a pk.
Upvotes: 5