使用 Yii2 迁移创建 table 时出错

error while creating table using Yii2 migration

我是 yii2 的新手,我正在使用 postgres,我的 db.php 文件是 -

return [
       'class' => 'yii\db\Connection',
       'dsn' => 'pgsql:host=localhost;port=5432;dbname=xxxx',
       'username' => 'postgres',
       'password' => 'abc',
       'charset' => 'utf8',
];

我直接在 postgres 中创建了一个 table,并使用 ActiveRecord 成功获取了数据。 然后我去创建一个 table 使用迁移

./yii migrate/create logins

成功在迁移文件夹中创建了一个文件,然后我将以下内容放入up方法-

public function up()
{
    $this->createTable('logins', [
            'id' => Schema::TYPE_PK,
            'name' => Schema::TYPE_STRING . ' NOT NULL',
            'password' => Schema::TYPE_STRING . ' NOT NULL'
    ]);
}

并触发 ./yii migrate 更新数据库,但我收到以下错误 -

Yii Migration Tool (based on Yii v2.0.10)
Exception 'yii\db\Exception' with message 'could not find driver'
in /opt/lampp/htdocs/project/server/api/project/vendor/yiisoft/yii2/db/Connection.php:549

我是否遗漏了任何步骤?或者 postgres 连接有问题?

我认为这是基本模板。

确保您已配置 db 组件。

在您的 config/console.php 文件中,检查 components 部分中是否有 db 键,例如:

// ...
'components' => [
    // ...
    'db' => require(__DIR__ . '/db.php'),
    // ...
],

如果一切正常但错误仍然存​​在,您需要检查是否已正确安装 pgsql 驱动程序。

详情见PDO Installation