Sequelize:在播种时查询 table 时关系“<tablename>”不存在
Sequelize : relation "<tablename>" does not exist when querying table while seeding
- Sequelize 连接给定的 sequelizerc / config.js 文件
- Sequelize 运行迁移文件并使用定义的模型创建 tables
- Sequelize 运行种子文件但在尝试查询创建的数据时出错即错误:关系"guest"不存在
我注意到的有趣的事情: 在查询用户 table 时,我注意到它没有 return 我期望的数据...而不是用户 table 上的用户,它似乎是 return 我在其上设置的实际 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 中有一个,所有 table 都已正确创建,并且种子数据已正确添加到数据库中。我尝试将模式添加到 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";
);
- Sequelize 连接给定的 sequelizerc / config.js 文件
- Sequelize 运行迁移文件并使用定义的模型创建 tables
- Sequelize 运行种子文件但在尝试查询创建的数据时出错即错误:关系"guest"不存在
我注意到的有趣的事情: 在查询用户 table 时,我注意到它没有 return 我期望的数据...而不是用户 table 上的用户,它似乎是 return 我在其上设置的实际 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 中有一个,所有 table 都已正确创建,并且种子数据已正确添加到数据库中。我尝试将模式添加到 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";
);