使用 sequelize-cli db:seed,访问 Postgres 时模式被忽略
using sequelize-cli db:seed, schema is ignored when accessing Postgres
我正在使用 express.js 构建 Web 服务并使用 Postgres 数据库进行 Sequilize。
数据库在架构 'schema1' 下保存 table 'country'。 Table 'country' 有字段 'name'、'isoCode'。
创建了一个种子文件以在 table 'country' 中插入国家列表。
种子文件看起来像:
'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) 上开了一张票,解决方法很简单:
不是仅将 table 设置为第一个参数,而是设置
{table名称:'country',架构:'schema1'}
您实际上可以通过对象指定架构和 table 名称,如 this Github issue:
中所述
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.bulkInsert(
{ tableName: 'account', schema: 'crm' },
{
name: 'Michael'
},
{}
);
},
down: function (queryInterface, Sequelize) {
return queryInterface.bulkDelete({ tableName: 'account', schema: 'crm' }, null, {});
}
};
我正在使用 express.js 构建 Web 服务并使用 Postgres 数据库进行 Sequilize。
数据库在架构 'schema1' 下保存 table 'country'。 Table 'country' 有字段 'name'、'isoCode'。
创建了一个种子文件以在 table 'country' 中插入国家列表。
种子文件看起来像:
'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) 上开了一张票,解决方法很简单:
不是仅将 table 设置为第一个参数,而是设置
{table名称:'country',架构:'schema1'}
您实际上可以通过对象指定架构和 table 名称,如 this Github issue:
中所述'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.bulkInsert(
{ tableName: 'account', schema: 'crm' },
{
name: 'Michael'
},
{}
);
},
down: function (queryInterface, Sequelize) {
return queryInterface.bulkDelete({ tableName: 'account', schema: 'crm' }, null, {});
}
};