NodeJS 多对多表没有连接

e5njpo68  于 2023-06-29  发布在  Node.js
关注(0)|答案(1)|浏览(134)

我正在尝试建立多对多关系,但总是遇到错误。
EagerLoadingError [SequelizeEagerLoadingError]:hints与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,
    }

在其中一份文件里,我...

HintsSections.findAll({
          where: { section_id: id },
          offset: offset,
          limit: limit,
          include: {
            model: Hints
          }
        });

我试着从HintsSections中删除引用,我试着将关联移动到HintsSections,但我一直得到相同的错误。我还尝试将所有内容合并到一个文件中,但表仍然没有建立连接。我不知道是什么错误。我该怎么办?

vs91vp4v

vs91vp4v1#

如果你想查询一个连接表,使用它的模型作为主模型,那么你需要显式地将所有需要的关联从HintsSections添加到链接表模型中:

HintsSections.belongsTo(Hints, { foreignKey: 'hint_id' });
 HintsSections.belongsTo(Sections, { foreignKey: 'section_id' });

当您查询Hints并包含Sections时,您已经定义的关联将起作用,反之亦然

相关问题