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

标签 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

我得到错误:

*** There was an error connecting to the database 'null:null@:0/null'. Reason: unable to connect to database.



我从源代码中的here跟踪了此错误消息。

最佳答案

事实证明这是一个简单的解决方法。我没有在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

关于postgresql - Dart Aqueduct服务器数据库升级:连接数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56119944/

相关文章:

sql - 使用 PostgreSQL 选择 json 中的特定条目

dart - For Each 循环在 dart 中创建数组

flutter - 登录在空上调用了 '_mulFromInteger'方法

dart - 如何在 Aqueduct 3 中启用 SQL 日志记录?

dart - Flutter - 将动态项目作为键传递

dart - Aqueduct 降级迁移版本

sql - 保证递归查询的唯一性

function - 使用 eval 函数返回表

java - 查询不从数据库返回任何内容

flutter - 仅更改一项项目的样式,而不是更改所有项目的样式。将所有 ListTile 更改为一个