Yii Migration 1215 Mysql Error

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

Possible: ON DELETE SET NULL on owner that doesn’t allow NULL? Also look in the table, does Yii pk() generate an int(11) or an int(10). I seem to remember MySQL generating an int(10) when AUTO_INCREMENT was declared, and int(11) if not.

OMG!!! shame on me, thanks a lot

I checked that before, pk generate int(11)

1215 is mysql error code

Sometimes the simple things get ya.

I ashamed of asking again, but really tolerance not reply me!!!

same error but different files

Codes in file "m140602_080318_create_table_umessage.php" which create message table:




public function safeUp()

	{

        /*

         * Create table umessage, this is connection way between customer & seller about specific object

         * Add foreign key to table user and it's column id with sender column

         * Add foreign key to table user and it's column id with reciever column

         * Add foreign key to table object and it's column id with objId column

         */

        $this->createTable('tbl_umessage', array(

            'id' => 'pk',

            'time' => 'INT(15) NOT NULL',

            'body' => 'TEXT NOT NULL',

            'status' => 'TINYINT NOT NULL DEFAULT 0',

            'visibleToS' => 'TINYINT NOT NULL DEFAULT 0',

            'visibleToR' => 'TINYINT NOT NULL DEFAULT 0',

            'sender' => 'INT(11)',

            'objId' => 'INT(11) NOT NULL',

        ), 'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci');

        

        $this->addForeignKey('fk_umessage_user', 'tbl_umessage', 'sender', 'tbl_user', 'id', 'SET NULL', 'CASCADE');

        

        $this->addForeignKey('fk_umessage_object', 'tbl_umessage', 'objId', 'tbl_object', 'id', 'CASCADE', 'CASCADE');

	}



And codes in file "m140602_080329_create_table_ureply.php" which create reply table:




public function safeUp()

	{

        /*

         * Create table ureply which store all replies to exact message

         * Add foreign key to table umessage and it's column id with msgId column

         * Add foreign key to table user and it's column id with sender column

         */

        $this->createTable('tbl_ureply', array(

            'id' => 'pk',

            'time' => 'INT(15) NOT NULL',

            'body' => 'TEXT NOT NULL',

            'msgId' => 'INT(11) NOT NULL',

            'sender' => 'INT(11)',

        ), 'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci');

        

        $this->addForeignKey('fk_ureply_umessage', 'tbl_ureply', 'msgId', 'umessage', 'id', 'CASCADE', 'CASCADE');

        

        $this->addForeignKey('fk_ureply_user', 'tbl_ureply', 'sender', 'tbl_user', 'id', 'SET NULL', 'CASCADE');

	}



Now I get same error while adding ‘fk_ureply_umessage’ and don’t know why I can’t find my silly goofs. I appreciate your helps.

Setting id=>pk to NULL. pk can’t be null. I think.