YII添加外键问题

The code in the book i was reading says to do the following .

run the below command :

yiic migrate create create_issue_user_and_assignment_tables

I ran the above command in protected directory .

and a file was create in the migrate folder , the book instructed , I inserted the following code in the created file :

public function safeUp()
    {
    //create the issue table
    $this->createTable('tbl_issue', array(
    'id' => 'pk',
    'name' => 'string NOT NULL',
    'description' => 'text',
    'project_id' => 'int(11) DEFAULT NULL',
    'type_id' => 'int(11) DEFAULT NULL',
    'status_id' => 'int(11) DEFAULT NULL',
    'owner_id' => 'int(11) DEFAULT NULL',
    'requester_id' => 'int(11) DEFAULT NULL',
    'create_time' => 'datetime DEFAULT NULL',
    'create_user_id' => 'int(11) DEFAULT NULL',
    'update_time' => 'datetime DEFAULT NULL',
    'update_user_id' => 'int(11) DEFAULT NULL',
    ), 'ENGINE=InnoDB');
    //create the user table
    $this->createTable('tbl_user', array(
    'id' => 'pk',
    'username' => 'string NOT NULL',
    'email' => 'string NOT NULL',
    'password' => 'string NOT NULL',
    'last_login_time' => 'datetime DEFAULT NULL',
    'create_time' => 'datetime DEFAULT NULL',
    'create_user_id' => 'int(11) DEFAULT NULL',
    'update_time' => 'datetime DEFAULT NULL',
    'update_user_id' => 'int(11) DEFAULT NULL',
    ), 'ENGINE=InnoDB');

    //create the assignment table that allows for many-to-many
    //relationship between projects and users
    $this->createTable('tbl_project_user_assignment', array(
    'project_id' => 'int(11) NOT NULL',
    'user_id' => 'int(11) NOT NULL',
    'PRIMARY KEY (`project_id`,`user_id`)',
    ), 'ENGINE=InnoDB');
    //foreign key relationships
    //the tbl_issue.project_id is a reference to tbl_project.id
    $this->addForeignKey("fk_issue_project", "tbl_issue", "project_
    id", "tbl_project", "id", "CASCADE", "RESTRICT");
    //the tbl_issue.owner_id is a reference to tbl_user.id
    $this->addForeignKey("fk_issue_owner", "tbl_issue", "owner_id",
    "tbl_user", "id", "CASCADE", "RESTRICT");
    //the tbl_issue.requester_id is a reference to tbl_user.id
    $this->addForeignKey("fk_issue_requester", "tbl_issue",
    "requester_id", "tbl_user", "id", "CASCADE", "RESTRICT");
    //the tbl_project_user_assignment.project_id is a reference to
    //tbl_project.id
    $this->addForeignKey("fk_project_user", "tbl_project_user_
    assignment", "project_id", "tbl_project", "id", "CASCADE",
    "RESTRICT");
    //the tbl_project_user_assignment.user_id is a reference to tbl_
    //user.id
    $this->addForeignKey("fk_user_project", "tbl_project_user_
    assignment", "user_id", "tbl_user", "id", "CASCADE", "RESTRICT");
    }
    public function safeDown()
    {
    $this->truncateTable('tbl_project_user_assignment');
    $this->truncateTable('tbl_issue');
    $this->truncateTable('tbl_user');
    $this->dropTable('tbl_project_user_assignment');
    $this->dropTable('tbl_issue');
    $this->dropTable('tbl_user');
    }

which i did , now in the command line i went and ran the following command:

yiic migrate

it gave me a migration successful message . but somehow the tables were not created , so i surfed SO for help and here SO Question

i found out that it would be wise to use the command up() and down() instead of safeUp() and safeDown() .

so i did that .. and well , i was almost successful , below is the screen shot of what happens when after changing the method names to up() nd down() , and i run the migrate command what happens .

enter image description here

now see how the tables_issue , table_user etc are created but somehow the creation of the foreign keys fails . i don't know why , i came across another thread that had an issue with creating foreign keys , but somehow , till it did't solve my problem .

foreign keys

I really appreciate your time and effort in answering my question.

You have used project_ id instead of project_id.