Dart Aqueduct服务器数据库升级:连接数据库时出错

时间:2019-05-13 21:12:55

标签: postgresql dart aqueduct

我正在为我的Aqueduct服务器设置一个PostgreSQL数据库。我使用psql创建了一个用户和数据库:

CREATE DATABASE words;
CREATE USER words_user WITH createdb;
ALTER USER words_user WITH password 'password';
GRANT all ON database words TO words_user;

我的模型课是

import 'package:demo/demo.dart';

class Word extends ManagedObject<_Word> implements _Word {

}

class _Word {
  @primaryKey
  int id;

  @Column(unique: true)
  String word;

  @Column()
  String info;
}

我用

生成了一个迁移文件
aqueduct db generate

这是:

import 'dart:async';
import 'package:aqueduct/aqueduct.dart';

class Migration1 extends Migration {
  @override
  Future upgrade() async {
    database.createTable(SchemaTable("_Word", [
      SchemaColumn("id", ManagedPropertyType.bigInteger,
          isPrimaryKey: true,
          autoincrement: true,
          isIndexed: false,
          isNullable: false,
          isUnique: false),
      SchemaColumn("word", ManagedPropertyType.string,
          isPrimaryKey: false,
          autoincrement: false,
          isIndexed: false,
          isNullable: false,
          isUnique: true),
      SchemaColumn("info", ManagedPropertyType.string,
          isPrimaryKey: false,
          autoincrement: false,
          isIndexed: false,
          isNullable: false,
          isUnique: false)
    ]));
  }

  @override
  Future downgrade() async {}

  @override
  Future seed() async {
    final rows = [
      {'word': 'horse', 'info': 'large animal you can ride'},
      {'word': 'cow', 'info': 'large animal you can milk'},
      {'word': 'camel', 'info': 'large animal with humps'},
      {'word': 'sheep', 'info': 'small animal with wool'},
      {'word': 'goat', 'info': 'small animal with horns'},
    ];

    for (final row in rows) {
      await database.store.execute(
          "INSERT INTO _Word (word, info) VALUES (@word, @info)",
          substitutionValues: {
            "word": row['word'],
            "info": row['info'],
          });
    }
  }
}

但是现在当我尝试通过

应用迁移时
aqueduct db upgrade --connect postgres:password@localhost:5432/words

我得到了错误:

  

***连接到数据库'null:null @:0 / null'时出错。原因:无法连接到数据库。

我跟踪到此错误消息来自源代码中的here

1 个答案:

答案 0 :(得分:0)

原来是一个简单的修复。我没有在CLI升级命令中包含数据库用户名。

aqueduct db upgrade --connect postgres://words_user:password@localhost:5432/words

通过比较我在documentation中为示例编写的内容,发现了它。

下一次我忘记使用正确的数据库名称时,也遇到了类似的错误。请记住包括所有部分。通用格式为

aqueduct db upgrade --connect postgres://username:password@host:port/databaseName

您可以通过输入

来获得CLI命令的帮助。
aqueduct db --help