" does not exist when querying table while seeding-6ren"> " does not exist when querying table while seeding-Sequelize 连接给定的 sequelizerc/config.js 文件 Sequelize 运行迁移文件并使用定义的模型创建表 Sequelize 运行种子文件但在尝试查询创建的数据时出错即-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:13:42 27 4
gpt4 key购买 nike

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

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

种子文件

var userId1 = uuidv4();
await queryInterface.bulkInsert(
{ tableName: 'User', schema: 'public' },
[
{email: "user1@email.com", 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;`
);

通过深入研究,它似乎可能没有使用正确的数据库模式?我在 public 下的 postgres 中有一个,所有表都正确创建,并且种子数据正确添加到数据库中。我尝试将模式添加到 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 = await queryInterface.sequelize.query( SELECT * from "Guest";);

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

27 4 0