Hello Dears;
After searching and checking my code, I was unable to find my code hole!!!
I created 2 table ‘user’ and ‘object’ with migrations file and in object migration file I want to add foreign key from ‘object’ to ‘user’
Codes in file “m140602_075932_create_table_user.php” which create ‘user’ table:
<?php
class m140602_075932_create_table_user extends CDbMigration
{
public function up()
{
/*
* Create user table
*/
$this->createTable('tbl_user', array(
'id' => 'pk',
'email' => 'VARCHAR(100) NOT NULL UNIQUE',
'password' => 'VARCHAR(30) NOT NULL',
'fname' => 'VARCHAR(30)',
'lname' => 'VARCHAR(30)',
'mobile' => 'VARCHAR(13)',
'image' => 'VARCHAR(30)',
'lastEntry' => 'INT(15)',
'status' => 'TINYINT NOT NULL DEFAULT 0',
), 'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci');
}
public function down()
{
/*
* Drop user table
*/
$this->dropTable('tbl_user');
}
}
And codes in file “m140602_075934_create_table_object.php” which create ‘object’ table:
<?php
class m140602_075934_create_table_object extends CDbMigration
{
public function safeUp()
{
/*
* Create object table
* Add foreign key to table group and it's column id with groupId column
* Add foreign key to table user and it's column id with owner column
*/
$this->createTable('tbl_object', array(
'id' => 'pk',
'name' => 'VARCHAR(255) NOT NULL',
'brand' => 'VARCHAR(255) NOT NULL',
'code' => 'VARCHAR(10) NOT NULL',
'price' => 'INT(15) NOT NULL',
'visit' => 'INT(5) NOT NULL DEFAULT 0',
'cash' => 'TINYINT NOT NULL DEFAULT 1',
'type' => 'TINYINT NOT NULL DEFAULT 0',
'status' => 'TINYINT NOT NULL DEFAULT 0',
'technical' => 'TEXT NOT NULL',
'description' => 'TEXT',
'tag' => 'TEXT',
'groupId' => 'INT(11) NOT NULL',
'owner' => 'INT(11) NOT NULL',
), 'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci');
$this->addForeignKey('fk_object_group', 'tbl_object', 'groupId', 'tbl_group', 'id', 'CASCADE', 'CASCADE');
$this->addForeignKey('fk_object_user', 'tbl_object', 'owner', 'tbl_user', 'id', 'SET NULL', 'CASCADE');
}
public function safeDown()
{
/*
* Drop foreign key which refer to group table
* Drop foreign key which refer to user table
* Drop table object
*/
$this->dropForeignKey('fk_object_group', 'tbl_object');
$this->dropForeignKey('fk_object_user', 'tbl_object');
$this->dropTable('tbl_object');
}
}
now when I want to execute “yiic migrate up 1” (refer to ‘m140602_075934_create_table_object.php’ file) table and ‘fk_object_group’ will be created successfully but for ‘fk_object_user’ I got mysql 1215 error.
I cant find my goofs, your help will be appreciated.
thanks in advance