Не связываются таблицы many to many

Пытаюсь сделать связь many to many но постоянно падает ошибка

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 Roles = require('./Roles');
const Users = require('./Users');
const News = require('./News');
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,
}

Затем в одном из файлов делаю

async getHints(id, limit = 10, offset = 0) {
    return await HintsSections.findAll({
      where: { section_id: id },
      offset: offset,
      limit: limit,
      include: {
        model: Hints
      }
    });
  }

Пробовал из HintsSections убирать references, пробовал в HintsSections переносить связь - постоянно одна и та же ошибка, как быть?


Ответы (0 шт):