2015-10-29 55 views
1

我正在使用express.js構建Web服務,並使用Postgres數據庫進行Sequilize。使用sequelize-cli db:seed,在訪問Postgres時忽略模式

數據庫在schema'schema1'下面保存着一個表'country'。表'國家'具有字段'名稱','isoCode'。

創建一個種子文件,在表'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:[email protected]: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

什麼是DB用戶的search_path?也許它不包括'schema1' –

+0

感謝您的回覆。執行: 顯示SEARCH_PATH 結果是: 「$用戶」,公共 –

+0

然後,當然不合格的表的名字不會被解析爲'schema1'。您可能需要更改用戶的搜索路徑以包含'schema1',教您的圖書館使用模式來限定表格,或者教您的圖書館如何在連接後動態更改搜索路徑 –

回答

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:[email protected]: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)機票並解決辦法很簡單:

,而不是隻將表作爲第一個參數,設置

{表名:「國家」,模式:'schema1'}