Yii Framework Forum: Error On Adding Foreign Keys Using Migrate - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Error On Adding Foreign Keys Using Migrate Rate Topic: -----

#1 User is offline   yaa110 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 20-December 13

Posted 20 December 2013 - 10:04 AM

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.

The image of the error has been attached.

Attached File(s)

  • Attached File  7tBJc.png (41.06K)
    Number of downloads: 8

0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 469
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 20 December 2013 - 11:02 AM

Which DBMS are you using?
0

#3 User is offline   yaa110 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 20-December 13

Posted 20 December 2013 - 11:48 AM

View PostCeBe, on 20 December 2013 - 11:02 AM, said:

Which DBMS are you using?


MySQL via WAMP on Windows 8.1
0

#4 User is offline   yaa110 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 20-December 13

Posted 20 December 2013 - 04:23 PM

The answer was found:
The reference of a foreign key has to be a Primary Key in other table.
0

#5 User is offline   jeremy07b 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 18-February 14

Posted 18 February 2014 - 05:22 AM

please help i have the same problem.. what shuold be done?
0

#6 User is offline   Gaurav Porwal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 03-April 14

Posted 11 April 2014 - 06:50 AM

I am also getting same error. when adding foreign key

$this->addForeignKey('FK_news', 'tbl_news', 'id', 'tbl_post', 'news_id');

Id in tbl_news is Primary Key.
0

#7 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 469
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 11 April 2014 - 06:56 AM

Your definition looks wrong.
Foreign key should be defined from post to news.
0

#8 User is offline   Gaurav Porwal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 03-April 14

Posted 11 April 2014 - 07:33 AM

View PostCeBe, on 11 April 2014 - 06:56 AM, said:

Your definition looks wrong.
Foreign key should be defined from post to news.



Yes you are right. Correct syntax is -

$this->addForeignKey('FK_news', 'tbl_post', 'news_id', 'tbl_news', 'id');


Thanks :-)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users