Sequelize.js 别名关联上的Sequelize eager加载错误

Sequelize.js 别名关联上的Sequelize eager加载错误,sequelize.js,eager-loading,model-associations,Sequelize.js,Eager Loading,Model Associations,嗨,我有下面的index.js var fs = require('fs'); var path = require('path'); var Sequelize = require('sequelize'); var CONFIG = require('../config/config'); var sequelize = new Sequelize(CONFIG.database, CONFIG.user, CONFIG.password, { host: CONFIG.host,

嗨,我有下面的index.js

var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var CONFIG = require('../config/config');

var sequelize = new Sequelize(CONFIG.database, CONFIG.user, CONFIG.password, {
  host: CONFIG.host,
  dialect: CONFIG.dialect,
  port: CONFIG.port,
  operatorsAliases: Sequelize.Op,
  logging: false
});

var db = {};


fs.readdirSync(__dirname)
  .filter(function (file) {
    return (file.indexOf('.') !== 0) && (file !== 'index.js');
  })
  .forEach(function (file) {
    var model = sequelize.import(path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(function (modelName) {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
我希望包含两个具有不同条件的模型,因此我计划在本例中使用alias:

module.exports = function (sequelize, DataTypes) {
  var TSection = sequelize.define('TSection', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    TestId: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      field: 'test_id',
      references: {
        model: 'test',
        key: 'id'
      }
    },
  },
  {
    tableName: 't_sections'
  });
  
  TSection.associate = function (models) {
    TSection.belongsTo(models.Test), {
      foreignKey: 'id',
      as: 'sections'
    },
    TSection.hasMany(models.TQuestion), {
      foreignKey: 'id'
    }
  };

  return TSection;
};

当我尝试获取以下信息时:

const test = await Test.findOne({
   attributes: ['id', 'name'],
   include: [{
     model: TSection,
       attributes: ['id', 'name'],
     }, {
     model: TSection,
       as: 'sections'
     }]
 });
我有这个错误,我不知道是什么原因造成的: 注意:我尝试了几种组合,包括和不包括上述模型

UnhandledPromiseRejectionWarning: Error: Error: SequelizeEagerLoadingError: TSection is associated to Test using an alias. You've included an alias (sections), but it does not match the alias(es) defined in your association (TSections).
如果您想知道,我的目标是计算总节数,但只检索一个节(与给定条件匹配的节)


谢谢

您在关联中添加了额外的括号。它们应该是这样的:

TSection.associate = function (models) {
    TSection.belongsTo(models.Test, {
      foreignKey: 'id',
      as: 'sections'
    });
    TSection.hasMany(models.TQuestion, {
      foreignKey: 'id'
    });
  };


非常感谢。那是我的问题。
TSection.associate = function (models) {
    TSection.belongsTo(models.Test, {
      foreignKey: 'id',
      as: 'sections'
    });
    TSection.hasMany(models.TQuestion, {
      foreignKey: 'id'
    });
  };
Test.associate = function (models) {
    Test.hasMany(models.TSection, {
      foreignKey: 'id'
    })
  }