Yii数据库迁移错误

时间:2013-12-31 18:18:55

标签: php yii

在phpmyadmin中创建数据库后,我想使用yii迁移工具。 我的迁移文件名为m131231_173214_create_project_table.php,内容如下:

class m131231_173214_create_project_table extends CDbMigration
{
    public function up()
    {
        $this->createTable(
            'tbl_project',
            array(
                'id'=>'pk',
                'name'=>'string NOT NULL',
                'description'=>'text NOT NULL',
                'create_time'=>'datetime DEFAULT NULL',
                'create_user_is'=>'int(11) DEFAULT NULL',
                'update_time'=>'datetime DEFAULT NULL',
                'update_user_id'=>'int(11) DEFAULT NULL',
            ),
            'ENGINE = InnoDB'
        );
    }

    public function down()
    {
        $this-dropTable('tbl_project');
    }

}

使用迁移工具后,我现在收到此错误:

*** applying m131231_173214_create_project_table
    > create table tbl_project ...exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: CDbCommand failed to prepare the SQL statement: SQLSTATE[HY000]: General error: 1 near "ENGINE": syntax error. The SQL statement executed was: CREATE TABLE 'tbl_project' (
        "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
        "name" varchar(255) NOT NULL,
        "description" text NOT NULL,
        "create_time" datetime DEFAULT NULL,
        "create_user_is" int(11) DEFAULT NULL,
        "update_time" datetime DEFAULT NULL,
        "update_user_id" int(11) DEFAULT NULL
) ENGINE = InnoDB' in /var/www/yii/framework/db/CDbCommand.php:358
Stack trace:
#0 /var/www/yii/framework/db/CDbCommand.php(1324): CDbCommand->execute()
#1 /var/www/yii/framework/db/CDbMigration.php(233): CDbCommand->createTable('tbl_project', Array, 'ENGINE = InnoDB')
#2 /var/www/yii/ts/protected/migrations/m131231_173214_create_project_table.php(19): CDbMigration->createTable('tbl_project', Array, 'ENGINE = InnoDB')
#3 /var/www/yii/framework/cli/commands/MigrateCommand.php(385): m131231_173214_create_project_table->up()
#4 /var/www/yii/framework/cli/commands/MigrateCommand.php(109): MigrateCommand->migrateUp('m131231_173214_...')
#5 [internal function]: MigrateCommand->actionUp(Array)
#6 /var/www/yii/framework/console/CConsoleCommand.php(172): ReflectionMethod->invokeArgs(Object(MigrateCommand), Array)
#7 /var/www/yii/framework/console/CConsoleCommandRunner.php(71): CConsoleCommand->run(Array)
#8 /var/www/yii/framework/console/CConsoleApplication.php(92): CConsoleCommandRunner->run(Array)
#9 /var/www/yii/framework/base/CApplication.php(180): CConsoleApplication->processRequest()
#10 /var/www/yii/framework/yiic.php(33): CApplication->run()
#11 /var/www/yii/ts/protected/yiic.php(7): require_once('/var/www/yii/fr...')
#12 /var/www/yii/ts/protected/yiic(4): require_once('/var/www/yii/ts...')

1 个答案:

答案 0 :(得分:0)

您的迁移代码是正确的。

但是,您的代码可能在MySQL上运行正确但在SQL-Lite上运行不正确(因为InnoDB是MySQL的数据库引擎)。

Yii迁移在命令行上运行,因此需要正确配置正确的配置文件。

$ more farecon.co.za/protected/config/console.php 
<?php

// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'My Console Application',

    // application components
    'components'=>array(
        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=testdrive',
            'emulatePrepare' => true,
            'username' => 'dbuser',
            'password' => 'dbpassword',
            'charset' => 'utf8',
        ),
    ),
);