我正在使用express.js和Sequilize使用Postgres DB构建Web服务。
数据库拥有一个表格' country'在架构' schema1'。表'国家'有字段' name',' isoCode'。
创建一个种子文件,以在表格#country;#39;中插入国家/地区列表。
种子文件看起来像:
'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)
我想我坚持这个。我很感激任何提供的帮助/指导等。
感谢您的时间。
答案 0 :(得分:3)
我在Postgres上执行了SQL查询:
ALTER ROLE <username> SET search_path TO schema1,public;
如上所述:Permanently Set Postgresql Schema Path
然后,再次成功地执行了播种机:
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'}
答案 1 :(得分:0)
您实际上可以通过对象来指定架构和表名,如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, {});
}
};