英文:
The many-to-many tables are not connecting
问题
我正在尝试建立多对多的关系,但我一直遇到一个错误。
> EagerLoadingError [SequelizeEagerLoadingError]: hints is not
> associated to hints_sections!
在其中一个文件中,我执行以下操作...
HintsSections.findAll({
where: { section_id: id },
offset: offset,
limit: limit,
include: {
model: Hints
}
});
我尝试删除HintsSections中的references,尝试将关联移到HintsSections,但我仍然收到相同的错误。我还尝试将所有内容合并到一个文件中,但表格仍然没有建立连接。我无法弄清楚错误是什么。我该怎么办?
英文:
I'm trying to establish a many-to-many relationship, but I keep encountering an error.
> EagerLoadingError [SequelizeEagerLoadingError]: hints is not
> associated to hints_sections!
Hints.js
const { sequelize, Sequelize } = require('./database');
const Hints = sequelize.define(
'hints',
{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
title: {
type: Sequelize.STRING(255),
allowNull: false,
},
content: {
type: Sequelize.STRING(5000),
allowNull: false,
},
user_id: {
type: Sequelize.INTEGER,
references: {
model: 'users',
key: 'id'
},
allowNull: false,
},
},{
timestamps: false,
});
module.exports = Hints;
Sections.js
const { sequelize, Sequelize } = require('./database');
const Sections = sequelize.define(
'sections',
{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
title: {
type: Sequelize.STRING(255),
allowNull: false,
},
parent_id: {
type: Sequelize.INTEGER,
references: {
model: 'sections',
key: 'id'
},
allowNull: true,
},
position: {
type: Sequelize.INTEGER,
allowNull: true,
},
},{
timestamps: false,
});
module.exports = Sections;
HintsSections.js
const { sequelize, Sequelize } = require('./database');
const Hints = require('./Hints');
const Sections = require('./Sections');
const HintsSections = sequelize.define(
'hints_sections',
{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
hint_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'hints',
key: 'id'
},
},
section_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'sections',
key: 'id'
},
},
},
{
timestamps: false,
freezeTableName: true,
});
module.exports = HintsSections;
index.js
const Hints = require('./Hints');
const Sections = require('./Sections');
const HintsSections = require('./HintsSections');
const { sequelize, Sequelize } = require('./database');
Hints.belongsToMany(Sections, { through: HintsSections, foreignKey: 'hint_id', otherKey: 'section_id', });
Sections.belongsToMany(Hints, { through: HintsSections, foreignKey: 'section_id', otherKey: 'hint_id', });
async function init() {
await sequelize.sync().then(async (result) => {
const response = await Roles.findAll({});
if(response.length < 3) {
Roles.create({ title: 'admin' });
Roles.create({ title: 'moderator' });
Roles.create({ title: 'user' });
}
});
}
init();
module.exports = {
Roles,
Users,
News,
Hints,
Sections,
HintsSections,
}
Then, in one of the files, I do...
HintsSections.findAll({
where: { section_id: id },
offset: offset,
limit: limit,
include: {
model: Hints
}
});
I tried removing references from HintsSections, I tried moving the association to HintsSections, but I keep getting the same error. I also attempted to consolidate everything into one file, but the tables still don't establish a connection. I can't figure out what the error is. What should I do?
答案1
得分: 0
如果您希望使用其模型作为主模型查询连接表,那么您需要明确添加从HintsSections
到链接表模型的所有必需关联关系:
HintsSections.belongsTo(Hints, { foreignKey: 'hint_id' });
HintsSections.belongsTo(Sections, { foreignKey: 'section_id' });
在查询Hints
并包括Sections
以及反之情况下,您已经定义的关联关系将起作用。
英文:
If you wish to query a junction table using its model as a main model then you need to add all needed associations from HintsSections
to linked table models explicitly:
HintsSections.belongsTo(Hints, { foreignKey: 'hint_id' });
HintsSections.belongsTo(Sections, { foreignKey: 'section_id' });
Your already defined associations will work in cases when you query Hints
and include Sections
and vice versa
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论