1366 Yii 2用户表迁移期间错误的字符串值错误

时间:2014-11-27 05:41:01

标签: php mysql yii2

我正在使用迁移脚本使用Yii 2的Migration创建和插入数据库值 当我迁移时,它会向我显示此错误:

*** applying m141125_045122_create_user_table
    > create table {{%user}} ... done (time: 0.009s)
    > insert into {{%user}} ...Exception 'yii\db\Exception' with message 'SQLSTA
TE[HY000]: General error: 1366 Incorrect string value: '\x9A\xD6M\xAE\x02g...' f
or column 'auth_key' at row 1
The SQL being executed was: INSERT INTO `user` (`username`, `password_hash`, `au
th_key`, `email`, `created_at`, `updated_at`) VALUES ('admin', '$2y$13$zKGigop9G
zmAkrfa8FSJIuebOOnNGDMsNuePwniQW60H7vnubaOwC', 'Ü╓M«☻gHt■Sα◄?↕ºo?Xèö▐ É¬=1N↑αï╨+
', 'admin@example.com', NOW(), NOW())'

in C:\Apache24\htdocs\media_avenue\vendor\yiisoft\yii2\db\Schema.php:532

Error Info:
Array
(
    [0] => HY000
    [1] => 1366
    [2] => Incorrect string value: '\x9A\xD6M\xAE\x02g...' for column 'auth_key'
 at row 1
)

这是我的迁移脚本:

public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8mb4  COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%user}}', [
            'id' => Schema::TYPE_PK,
            'username' => Schema::TYPE_STRING . ' NOT NULL',
            'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL',
            'password_hash' => Schema::TYPE_STRING . ' NOT NULL',
            'password_reset_token' => Schema::TYPE_STRING,
            'email' => Schema::TYPE_STRING . ' NOT NULL',
            'role' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10',

            'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10',
            'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',
            'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL',
        ], $tableOptions);

        $this->insert('{{%user}}', [
            'username' => 'admin',
            'password_hash' => Yii::$app->security->generatePasswordHash('admin'),
            'auth_key' => Yii::$app->security->generateRandomKey(),
            'email'=> Yii::$app->params['adminEmail'],
            'created_at'=> new Expression('NOW()'),
            'updated_at'=> new Expression('NOW()'),
        ]);
    }

我的MySQL版本是5.6.21.1, 我已经尝试通过将其从utf8更改为utf8mb4来搞乱编码,但它仍然无法正常工作

这可能是什么原因?

之前谢谢

注意:

  • 如果我使用XAMPP,我不会收到此错误。

  • 只有在我手动安装MySQL

  • 时才会显示错误