【问题标题】:using sequelize-cli db:seed, schema is ignored when accessing Postgres使用 sequelize-cli db:seed,访问 Postgres 时忽略架构
【发布时间】:2015-10-29 15:32:47
【问题描述】:

我正在使用 express.js 和 Sequilize 与 Postgres DB 构建 Web 服务。

数据库在模式“schema1”下保存一个表“国家”。表 'country' 包含字段 'name'、'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)

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

感谢您的宝贵时间。

【问题讨论】:

  • DB 用户的search_path 是什么?也许它不包括schema1
  • 感谢您的回复。执行:show search_path 结果是:"$user", public
  • 那么当然不会将不合格的表名解析为schema1。您需要更改用户的搜索路径以包含 schema1,教您的库使用架构限定表,或者教您的库如何在连接后动态更改搜索路径
  • 感谢有关“搜索路径”的信息

标签: postgresql express sequelize.js seeding


【解决方案1】:

我在 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'}

【讨论】:

    【解决方案2】:

    您实际上可以通过对象指定架构和表名,如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, {});
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2012-05-22
      • 1970-01-01
      • 2019-03-25
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      相关资源
      最近更新 更多