使用ENGINE子句在迁移中创建表失败

问题描述:

I am trying to do a migration using Yii and create new table in up() method. It works fine as long as I don't add the ENGINE=InnoDB clause. In that case - it gives me an error near ENGINE.

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_id' => 'int(11) DEFAULT NULL',
        'update_time' => 'datetime DEFAULT NULL',
        'update_user_id' => 'int(11) DEFAULT NULL',
    ), 'ENGINE=InnoDB');
}

My version of Yii is 1.1.12. PHP 5.4.3, MySQL 5.5.24.

Is that some Yii bug?

EDIT (yii bug description):

*** applying m130208_133533_create_table_project
> create table tbl_project ...exception 'CDbException' with message 'CDbComm
and failed to execute the SQL statement: CDbCommand failed to prepare the SQL st
atement: 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_id" int(11) DEFAULT NULL,
    "update_time" datetime DEFAULT NULL,
    "update_user_id" int(11) DEFAULT NULL
) engine = InnoDB' in C:\wamp\yii\framework\db\CDbCommand.php:357

Using "protected\yiic\migrate", being a console command, uses protected\config\console.php, so its "db" component should be configured properly (as protected\config\main.php was configured). If you get the InnoDB error, chances are you are still configured for SQLite.

Try:

$this->createTable('tbl_project', array(
        'id' => 'pk',
        'name' => 'string NOT NULL',
        'description' => 'text NOT 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'
);

If it doesn't work then show us the MySql error

I had the same problem and it was solved by checking that the configuration of the database in console.php & main.php are the same

i.e 'db'=>array(
        'connectionString' => 'mysql:host=localhost;dbname=databasename',
        'emulatePrepare' => true,
        'username' => 'root',
        'password' => 'ur password',
        'charset' => 'utf8',
    ),

and comment the sqlite lines in both files

Another approach is to include on .env file:

DB_ENGINE=InnoDB