[英]Dart Aqueduct server database upgrade: error connecting to the database
I'm setting up a PostgreSQL database for my Aqueduct server.我正在为我的 Aqueduct 服务器设置一个 PostgreSQL 数据库。 I created a user and database with psql
:我用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;
My model class is我的模型类是
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;
}
I generated a migration file with我生成了一个迁移文件
aqueduct db generate
which is:即:
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'],
});
}
}
}
But now when I try to apply the migration with但是现在当我尝试应用迁移时
aqueduct db upgrade --connect postgres:password@localhost:5432/words
I get the error:我收到错误:
*** There was an error connecting to the database 'null:null@:0/null'. *** 连接到数据库“null:null@:0/null”时出错。 Reason: unable to connect to database.原因:无法连接到数据库。
I tracked this error message as coming from here in the source code.我在源代码中跟踪此错误消息来自此处。
It turned out to be a simple fix.结果证明这是一个简单的修复。 I hadn't included the database user name in the CLI upgrade command.我没有在 CLI 升级命令中包含数据库用户名。
aqueduct db upgrade --connect postgres://words_user:password@localhost:5432/words
I found it by comparing what I had written to the example in the documentation .我通过比较我写的内容与文档中的示例找到了它。
I got a similar error another time when I forgot to use the right database name.当我忘记使用正确的数据库名称时,我又遇到了类似的错误。 And another time when I had a space before the password.还有一次我在密码前有一个空格。 Remember to include all the parts and make sure that everything is formatted correctly.请记住包括所有部分,并确保所有内容的格式正确。 The general format is一般格式是
aqueduct db upgrade --connect postgres://username:password@host:port/databaseName
You can get help with the CLI command by typing您可以通过键入来获取 CLI 命令的帮助
aqueduct db --help
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.