老实说,我在这个问题上真的很迷茫。我有四个PostgreSQL表,我试图通过Node/Express/Sequelize查询。我使用Sequelize-CLI自动生成的模型创建了这四个表,并使用同一Sequelize-CLI自动生成的迁移模板迁移了这些表。当我通过PSQL访问这些表时,正确的外键连接确实存在。下面是四个表中相关的两个:
Program.js
'use strict';
const { Model, DataTypes } = require('sequelize');
module.exports = (sequelize) => {
class Program extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
this.hasMany(models.Track, {
foreignKey: 'program_id'
});
this.belongsTo(models.Cart);
}
}
Program.init({
program_id: {
primaryKey: true,
type: DataTypes.UUID,
allowNull: false
},
program_length_ms: DataTypes.BIGINT,
intra_track_fade_length_ms: DataTypes.BIGINT
}, {
sequelize,
modelName: 'Program',
});
return Program;
};
Track.js
'use strict';
const { Model, DataTypes } = require('sequelize');
module.exports = (sequelize) => {
class Track extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
this.belongsTo(models.Program, {
foreignKey: 'program_id'
});
}
}
Track.init({
track_id: {
primaryKey: true,
type: DataTypes.UUID,
allowNull: false
},
spotify_track_id: {
type: DataTypes.STRING,
allowNull: false
},
program_id: {
type: DataTypes.UUID,
allowNull: false
},
program_position: DataTypes.BIGINT,
track_name: DataTypes.STRING,
duration_ms: DataTypes.BIGINT
}, {
sequelize,
modelName: 'Track',
});
return Track;
};
以下是相关的Sequelize-CLI迁移脚本:
create-program.js
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Programs', {
program_id: {
primaryKey: true,
type: Sequelize.UUID,
allowNull: false
},
program_length_ms: {
type: Sequelize.BIGINT
},
intra_track_fade_length_ms: {
type: Sequelize.BIGINT
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Programs');
}
};
create-track.js
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Tracks', {
track_id: {
primaryKey: true,
allowNull: false,
type: Sequelize.UUID
},
spotify_track_id: {
type: Sequelize.STRING,
allowNull: false
},
program_id: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: {
tableName: 'Programs'
},
key: 'program_id'
}
},
program_position: {
type: Sequelize.BIGINT
},
track_name: {
type: Sequelize.STRING
},
duration_ms: {
type: Sequelize.BIGINT
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Tracks');
}
};
下面是后端Node/Express脚本的片段(嵌套在事务中),我希望使用它来产生与SELECT * FROM "Programs" JOIN "Tracks" ON "Tracks.program_id" = "Programs.program_id"
基本相同的结果:
const resultLibrary = await Program.findAll({
include: [
Track
]
});
谁能告诉我我做错了什么吗
尝试执行上述文本中包含的语句。本应从Sequelize ORM接收嵌套JSON输出,但收到的却是:
Executing (a63c5e44-d14e-4e55-b047-3f685c0be5bf): START TRANSACTION;
Executing (a63c5e44-d14e-4e55-b047-3f685c0be5bf): ROLLBACK;
Error while fetching user cart library: EagerLoadingError [SequelizeEagerLoadingError]: Track is not associated to Program!
at Program._getIncludedAssociation (/Users/administrator/Documents/vscode_projects/8-track-using-spotify/server/node_modules/sequelize/lib/model.js:565:13)
at Program._validateIncludedElement (/Users/administrator/Documents/vscode_projects/8-track-using-spotify/server/node_modules/sequelize/lib/model.js:502:53)
at /Users/administrator/Documents/vscode_projects/8-track-using-spotify/server/node_modules/sequelize/lib/model.js:421:37
at Array.map (<anonymous>)
at Program._validateIncludedElements (/Users/administrator/Documents/vscode_projects/8-track-using-spotify/server/node_modules/sequelize/lib/model.js:417:39)
at Program.findAll (/Users/administrator/Documents/vscode_projects/8-track-using-spotify/server/node_modules/sequelize/lib/model.js:1118:12)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /Users/administrator/Documents/vscode_projects/8-track-using-spotify/server/controllers/library.js:44:26
at async /Users/administrator/Documents/vscode_projects/8-track-using-spotify/server/node_modules/sequelize/lib/sequelize.js:507:18
at async getLibrary (/Users/administrator/Documents/vscode_projects/8-track-using-spotify/server/controllers/library.js:25:18)
1条答案
按热度按时间fquxozlt1#
我真的很震惊,Anthony Barragan(SequelizeEagerLoadingError with Sequelized)的答案也适用于我的情况:当使用Sequelize-CLI生成的helper函数时,所有模型都必须按照该答案中的指定导入,以便正常运行。