gpt4 book ai didi

postgresql - 使用 sequelize-cli db :seed, 模式在访问 Postgres 时被忽略

转载 作者:行者123 更新时间:2023-11-29 12:44:17 26 4
gpt4 key购买 nike

我正在使用 express.js 和带有 Postgres 数据库的 Sequilize 构建 Web 服务。

数据库在模式“schema1”下保存了一个表“country”。表“国家”有字段“名称”、“isoCode”。

创建了一个种子文件以在“国家”表中插入国家列表。

种子文件看起来像:

'use strict';

module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.bulkInsert(
'country',
[
{
"name":"Afghanistan",
"isoCode":"AF"
},
{
"name":"Åland Islands",
"isoCode":"AX"
},
{
"name":"Albania",
"isoCode":"AL"
},
{
"name":"Algeria",
"isoCode":"DZ"
},
{
"name":"American Samoa",
"isoCode":"AS"
},
{
"name":"Andorra",
"isoCode":"AD"
}
],
{
schema : 'schema1'
}
);
},

down: function (queryInterface, Sequelize) {

}
};

在运行种子时我得到这个错误:

    node_modules/sequelize-cli/bin/sequelize --url postgres://user:password@localhost:5432/database db:seed

Sequelize [Node: 0.12.6, CLI: 2.0.0, ORM: 3.11.0, pg: ^4.4.2]

Parsed url postgres://user:*****@localhost:5432/database
Starting 'db:seed'...
Finished 'db:seed' after 165 ms
== 20151029161319-Countries: migrating =======
Unhandled rejection SequelizeDatabaseError: relation "country" does not exist
at Query.formatError (node_modules/sequelize/lib/dialects/postgres/query.js:437:14)
at null.<anonymous> (node_modules/sequelize/lib/dialects/postgres/query.js:112:19)
at emit (events.js:107:17)
at Query.handleError (node_modules/pg/lib/query.js:108:8)
at null.<anonymous> (node_modules/pg/lib/client.js:171:26)
at emit (events.js:107:17)
at Socket.<anonymous> (node_modules/pg/lib/connection.js:109:12)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)

我想我被困在这个问题上了。我将不胜感激任何提供的帮助/指导等。

感谢您的宝贵时间。

最佳答案

我在 Postgres 上执行了 SQL 查询:

     ALTER ROLE <username> SET search_path TO schema1,public;

如此处所述:Permanently Set Postgresql Schema Path

然后,再次成功执行seeder:

    node_modules/sequelize-cli/bin/sequelize --url postgres://user:password@localhost:5432/database db:seed

Sequelize [Node: 0.12.6, CLI: 2.0.0, ORM: 3.11.0, pg: ^4.4.2]

Parsed url postgres://user:*****@localhost:5432/database
Using gulpfile node_modules/sequelize-cli/lib/gulpfile.js
Starting 'db:seed'...
Finished 'db:seed' after 558 ms
== 20151029161319-Countries: migrating =======
== 20151029161319-Countries: migrated (0.294s)

感谢@a_horse_with_no_name 提供有关 search_path 的信息。我希望 sequelize 库可以处理这种情况,或者我可能误用了它。

更新:在 Github (https://github.com/sequelize/sequelize/issues/4778#issuecomment-152566806) 上开了一张票,解决方案很简单:

不是只将表格设置为第一个参数,而是设置

{tableName: 'country', schema : 'schema1'}

关于postgresql - 使用 sequelize-cli db :seed, 模式在访问 Postgres 时被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33418459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com