A
A
Anton Natarov2015-12-10 01:16:56
PostgreSQL
Anton Natarov, 2015-12-10 01:16:56

Yii2 postgres how to implement start migration?

Implemented the Yii2 Advanced setup. Config set to postgresql. next.

'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'pgsql:host=localhost;dbname=ххх',
            'username' => 'postgres',
            'password' => '',
            'charset' => 'utf8',
        ],

the migration itself:
<?php

use yii\db\Schema;
use yii\db\Migration;

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

        $this->createTable('{{%user}}', [
            'id' => $this->primaryKey(),
            'username' => $this->string()->notNull()->unique(),
            'auth_key' => $this->string(32)->notNull(),
            'password_hash' => $this->string()->notNull(),
            'password_reset_token' => $this->string()->unique(),
            'email' => $this->string()->notNull()->unique(),

            'status' => $this->smallInteger()->notNull()->defaultValue(10),
            'created_at' => $this->integer()->notNull(),
            'updated_at' => $this->integer()->notNull(),
        ], $tableOptions);
    }

    public function down()
    {
        $this->dropTable('{{%user}}');
    }
}

I use the Yii migrate command in the console, after which it finds a migration that has not been added and offers to add it, to which I agree. But in the end, the console complains about a syntax error in this array
CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB

Who faced, how did you decide? The problem is not in the connection config. in basis the table Migrate created. Users cannot implement.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Donkovtsev, 2015-12-10
@HanDroid

An exception is thrown because PostgreSQL != MySQL, your PostgreSQL query must not have this string.

A
Andrey Filatov, 2018-02-17
@filatoff

SQL command 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB' is relevant only for MySQL.
Posqtresql knows nothing about InnoDB, MyISAM, etc. engines.
Comment out the if block with this command and everything should work out

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question