Yii迁移,不创建表


Yii Migration, Tables are not created

我是Yii(Still Learning)的新手,我正在学习一本书的教程在这里,我按照书中所写的创建了一个新的迁移

yiic migrate create create_issue_user_and_assignment_tables

在保险箱里我写了这个查询

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

这个在safeDown()中

$this->dropTable('tbl_issue');
$this->dropTable('tbl_user');

然后运行它并得到以下消息

D:'wamp'www'yiisite'protected>yiic migrate
PHP Deprecated:  Directive 'register_globals' is deprecated in PHP 5.3 and great
er in Unknown on line 0
Deprecated: Directive 'register_globals' is deprecated in PHP 5.3 and greater in
 Unknown on line 0
Yii Migration Tool v1.0 (based on Yii v1.1.13)
Total 1 new migration to be applied:
    m130703_085302_create_issue_user_and_assignment_tables
Apply the above migration? (yes|no) [no]:yes
*** applying m130703_085302_create_issue_user_and_assignment_tables
*** applied m130703_085302_create_issue_user_and_assignment_tables (time: 0.042s
)

Migrated up successfully.

现在的问题是,数据库中没有创建表,这可能是因为register_globals被弃用的消息,但我不确定该怎么办,连接参数是正确的,并且在tbl_migration 表中插入了一条记录

m130703_085302_create_issue_user_and_assignment_ta...   1372842220

但是没有创建新的表。

创建表通常不需要事务

<?php
class m130630_124600_some_description_name extends CDbMigration
{
    public function up(){
        //upcode example create the session table
        $this->createTable('session',[
             'id' => "varchar(40) NOT NULL",
             'expire' => "int(12)",
             'data' => "blob",
        ]);
        $this->addPrimaryKey('idx','session','id');
    }
    public function down(){
       // downcode (undo the up code) example: drop session table
       $this->dropTable('session');
    }
}

如果需要交易

遵循safeUp的注释:

此方法包含应用此时要执行的逻辑迁移此方法与up()的不同之处在于DB逻辑在这里实现的将包含在DB事务中。小孩如果DB逻辑需要在事务中。

检查config/console.php文件更改数据库名称、用户名和密码。这是yiic命令使用的文件,而不是config/main.php它现在应该可以工作了

也许你做错了,我做了什么。yiic是一个控制台命令。在项目的config文件夹中检查console.php的数据库属性。检查控制台的配置。

它可能指向testdrive.db SQLite默认的一个&表是由他们创建的。

至少这是我的问题。

问候,Ajeet

我刚刚在其中对您的代码进行了干运行,它创建了一些表,所以我不得不假设您的调用有问题。唯一直接突出的是,您运行的是bash脚本,而不是bat脚本。

yiic是一个bash脚本。在Windows上,建议运行yiic.batyiic.php.

你能从命令行运行php吗?如果是:

  • 从tbl_migrations中删除迁移
  • 以"C:''path''to''php.exe yiic.php migrate up"的方式运行迁移

确保您没有通过MySQL手动创建tbl_usertbl_issue表。如果它们已经存在,请删除它们,然后再次运行迁移。

还要检查以确保main/config.phpmain/console.php中的数据库连接字符串已设置为正确引用迁移应使用的数据库。

为了确认它是否有效,当您的迁移真正成功时,您将得到与下面非常相似的结果,列出所采取的每个SQL操作。如果您在列表中没有看到任何操作,则说明它没有正确运行。

D:'xampp'htdocs'yii'trackstar'protected>yiic migrate
Yii Migration Tool v1.0 (based on Yii v1.1.14)
Total 1 new migration to be applied:
    m140406_014347_create_user_issue_assignment_tables
Apply the above migration? (yes|no) [no]:y
*** applying m140406_014347_create_user_issue_assignment_tables
    > create table tbl_issue ... done (time: 0.351s)
    > create table tbl_user ... done (time: 0.405s)
    > create table tbl_project_user_assignment ... done (time: 0.366s)
    > add foreign key fk_issue_project: tbl_issue (project_id) references tbl_pr
oject (id) ... done (time: 0.923s)
    > add foreign key fk_issue_owner: tbl_issue (owner_id) references tbl_user (
id) ... done (time: 1.066s)
    > add foreign key fk_issue_requester: tbl_issue (requester_id) references tb
l_user (id) ... done (time: 1.829s)
    > add foreign key fk_project_user: tbl_project_user_assignment (project_id)
references tbl_project (id) ... done (time: 1.416s)
    > add foreign key fk_user_project: tbl_project_user_assignment (user_id) ref
erences tbl_user (id) ... done (time: 1.032s)
*** applied m140406_014347_create_user_issue_assignment_tables (time: 7.446s)

Migrated up successfully.

最后,在MySQL中创建表将在运行后隐式提交,因此在safeDown()safeUp()方法中运行代码并不是特别有用,因为它们将把代码作为MySQL事务运行。为了简单起见,我将把每个安全方法中的代码移到各自的"非安全"方法中。

也许你做错了,我做了什么。yiic是一个控制台命令。在项目的config文件夹中检查console.php的数据库属性。

它可能指向testdrive.db SQLite默认的一个&表是由他们创建的。

至少这是我的问题。

谨致问候,Ajeet

我也遇到了同样的问题。这是通过将代码移动到来解决的

公共函数up(){此处输入代码}

并通过以下命令执行迁移

shell>yiic向上迁移