YII添加外键问题

时间:2014-11-28 06:46:32

标签: php mysql yii migrate

我正在阅读的书中的代码说要做以下事情。

运行以下命令:

yiic migrate create create_issue_user_and_assignment_tables

我在受保护的目录中运行了上述命令。

并且在migrate文件夹中创建了一个文件,书中指示,我在创建的文件中插入了以下代码:

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');
    }

我做了,现在在命令行中我去了并运行了以下命令:

yiic migrate

它给了我一个迁移成功的消息。但不知何故,表格没有创建,所以我在SO上寻求帮助,SO Question

我发现使用命令up()和down()而不是safeUp()和safeDown()是明智的。

所以我这样做了......好吧,我几乎成功了,下面是将方法名称更改为up()nd down()后发生的事情的屏幕截图,然后我运行migrate命令会发生什么。

enter image description here

现在看看table_issue,table_user等是如何创建的,但是以某种方式创建外键失败了。我不知道为什么,我遇到了另一个创建外键问题的线程,但不知怎的,直到它没有解决我的问题。

foreign keys

我非常感谢您回答我的问题所花费的时间和精力。

1 个答案:

答案 0 :(得分:0)

您使用了project_ id代替project_id