node.js - Sequelize : relation "<tablename>" does not exist when querying table while seeding

标签 node.js postgresql sequelize.js hapi.js

  1. Sequelize 与给定的sequelizerc/config.js 文件连接
  2. Sequelize 运行迁移文件并使用定义的模型创建表
  3. Sequelize 运行种子文件但在尝试查询创建的数据时出错,即错误:关系“guest”不存在

我注意到的有趣的事情:在查询 User 表时,我注意到它没有返回我期望的数据......而不是 User 表上的用户,它似乎返回我在其上设置的实际 postgres 数据库用户。

播种机文件

var userId1 = uuidv4();
await queryInterface.bulkInsert(
  { tableName: 'User', schema: 'public' }, 
  [
    {email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92e7e1f7e0a3d2f7fff3fbfebcf1fdff" rel="noreferrer noopener nofollow">[email protected]</a>", id:userId1,  password: User.prototype.generateHash("password"), createdAt:dateUtil.createDateAsUTC(new Date()), updatedAt:dateUtil.createDateAsUTC(new Date())}
  ], {}
);

const user = await queryInterface.sequelize.query(
  `SELECT * from USER;`
);

console.log(user);

var guestId1 = uuidv4();
await queryInterface.bulkInsert(
  { tableName: 'Guest', schema: 'public' }, 
  [
    {id:guestId1, userId: userId1, createdAt:dateUtil.createDateAsUTC(new Date()), updatedAt:dateUtil.createDateAsUTC(new Date())}
  ], {}
);

const guest = await queryInterface.sequelize.query(
  `SELECT * from Guest;`
);

从周围挖掘看来,它可能没有使用正确的数据库模式?我在 postgres 的 public 下有一个,所有表都已正确创建,并且种子数据已正确添加到数据库中。我尝试将架构添加到 config.js 和模型文件中,但没有任何效果。

CONFIG.JS

development: {
        username: process.env.DATABASE_USERNAME,
        password: process.env.DATABASE_PASSWORD,
        database: process.env.DATABASE_NAME,
        host: process.env.DATABASE_HOST,
        url: process.env.DATABASE_URL,
        dialect: 'postgres',
        define: {
            schema: "public"
        }
    }

Guest.js

module.exports = (sequelize, DataTypes) => {
  var Guest = sequelize.define('Guest', {}, 
  {
    tableName:"Guest",
  });
  Guest.associate = function(models) {
    Guest.belongsTo(models.User, {as: 'User', foreignKey: 'userId'}); // Adds userId 
  };
  return Guest;
};

注销时queryInterface模型似乎是空的......

QueryInterface {
  sequelize:
   Sequelize {
     options:
      { dialect: 'postgres',
        dialectModulePath: null,
        host: 'localhost',
        protocol: 'postgres',
        define: {},
        query: {},
        sync: {},
        timezone: '+00:00',
        logging: [Function: logMigrator],
        omitNull: false,
        native: false,
        replication: false,
        ssl: false,
        pool: {},
        quoteIdentifiers: true,
        hooks: {},
        retry: [Object],
        transactionType: 'DEFERRED',
        isolationLevel: null,
        databaseVersion: '9.4.0',
        typeValidation: false,
        benchmark: false,
        operatorsAliases: true,
        username: 'username',
        password: 'password',
        database: 'dbname',
        url: 'postgres://username:password@localhost:5432/dbname',
        port: '5432' },
     config:
      { database: '<dbname>',
        username: '<username>',
        password: 'password',
        host: 'localhost',
        port: '5432',
        pool: {},
        protocol: 'postgres',
        native: false,
        ssl: false,
        replication: false,
        dialectModulePath: null,
        keepDefaultTimezone: undefined,
        dialectOptions: undefined },
     dialect:
      PostgresDialect {
        sequelize: [Circular],
        connectionManager: [Object],
        QueryGenerator: [Object] },
     queryInterface: [Circular],
     models: {},
     modelManager: ModelManager { models: [], sequelize: [Circular] }

最佳答案

更正了表名周围双引号的问题:

const guest =等待queryInterface.sequelize.query( 从“客人”中选择*; );

关于node.js - Sequelize : relation "<tablename>" does not exist when querying table while seeding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51660939/

相关文章:

python - 如何使用 ponyorm 中的实体进行 get() 查询

javascript - 是否可以在 sequelize 中基于关联进行查询?

node.js - Azure Blob 存储默认压缩文件?

node.js - Ubuntu apt-get 未满足的 NPM 依赖项

sql - 获取列中逗号分隔值的最大值

sql - 对从 postgresql 中的查询获得的值进行排序

javascript - Sequelize Model.increment 返回 [undefined, number] 而不是受影响的行

node.js - Sequelize Node.js - 嵌套对象

javascript - Node js 需要使用变量 (var) 的 json

node.js - Mongoose 选择 !boolean 或 boolean 和过去的日期