【问题标题】:Sequelize Association called with something that's not a subclass of Sequelize.Model使用不是 Sequelize.Model 子类的东西调用 Sequelize 关联
【发布时间】:2019-05-21 18:40:26
【问题描述】:

当我在我的模型中添加 Sequelize 的关联时,我遇到错误“...调用不是 Sequelize.Model 的子类”,它称为错误,我称之为不是 Sequelize 模型

E:...\Projects\WebApps\hr1\hr1\node_modules\sequelize\lib\associations\mixin.js:81
      throw new Error(this.name + '.' + Utils.lowercaseFirst(Type.toString()) + ' called with something that\'s not a subclass of Sequelize.Model');
      ^

Error: user_employee_tm.class BelongsTo extends Association {
  constructor(source, target, options) {
    super(source, target, options);

    this.associationType = 'BelongsTo';
    this.isSingleAssociation = true;
    this.foreignKeyAttribute = {};

    if (this.as) {
      this.isAliased = true;
      this.options.name = {
        singular: this.as
      };
    } else {
      this.as = this.target.options.name.singular;
      this.options.name = this.target.options.name;
    }

    if (_.isObject(this.options.foreignKey)) {
      this.foreignKeyAttribute = this.options.foreignKey;
      this.foreignKey = this.foreignKeyAttribute.name || this.foreignKeyAttribute.fieldName;
    } else if (this.options.foreignKey) {
      this.foreignKey = this.options.foreignKey;
    }

    if (!this.foreignKey) {
      this.foreignKey = Utils.camelizeIf(
        [
          Utils.underscoredIf(this.as, this.source.options.underscored),
          this.target.primaryKeyAttribute
        ].join('_'),
        !this.source.options.underscored
      );
    }

    this.identifier = this.foreignKey;

    if (this.source.rawAttributes[this.identifier]) {
      this.identifierField = this.source.rawAttributes[this.identifier].field || this.identifier;
    }

    this.targetKey = this.options.targetKey || this.target.primaryKeyAttribute;
    this.targetKeyField = this.target.rawAttributes[this.targetKey].field || this.targetKey;
    this.targetKeyIsPrimary = this.targetKey === this.target.primaryKeyAttribute;

    this.targetIdentifier = this.targetKey;
    this.associationAccessor = this.as;
    this.options.useHooks = options.useHooks;

    // Get singular name, trying to uppercase the first letter, unless the model forbids it
    const singular = Utils.uppercaseFirst(this.options.name.singular);

    this.accessors = {
      get: 'get' + singular,
      set: 'set' + singular,
      create: 'create' + singular
    };
  }

  // the id is in the source table
  injectAttributes() {
    const newAttributes = {};

    newAttributes[this.foreignKey] = _.defaults({}, this.foreignKeyAttribute, {
      type: this.options.keyType || this.target.rawAttributes[this.targetKey].type,
      allowNull: true
    });

    if (this.options.constraints !== false) {
      const source = this.source.rawAttributes[this.foreignKey] || newAttributes[this.foreignKey];
      this.options.onDelete = this.options.onDelete || (source.allowNull ? 'SET NULL' : 'NO ACTION');
      this.options.onUpdate = this.options.onUpdate || 'CASCADE';
    }

    Helpers.addForeignKeyConstraints(newAttributes[this.foreignKey], this.target, this.source, this.options, this.targetKeyField);
    Utils.mergeDefaults(this.source.rawAttributes, newAttributes);

    this.identifierField = this.source.rawAttributes[this.foreignKey].field || this.foreignKey;

    this.source.refreshAttributes();

    Helpers.checkNamingCollision(this);

    return this;
  }

  mixin(obj) {
    const methods = ['get', 'set', 'create'];

    Helpers.mixinMethods(this, obj, methods);
  }

  /**
   * Get the associated instance.
   *
   * @param {Object} [options]
   * @param {String|Boolean} [options.scope] Apply a scope on the related model, or remove its default scope by passing false.
   * @param {String} [options.schema] Apply a schema on the related model
   * @see {@link Model.findOne} for a full explanation of options
   * @return {Promise<Model>}
   */
  get(instances, options) {
    const association = this;
    const where = {};
    let Target = association.target;
    let instance;

    options = Utils.cloneDeep(options);

    if (options.hasOwnProperty('scope')) {
      if (!options.scope) {
        Target = Target.unscoped();
      } else {
        Target = Target.scope(options.scope);
      }
    }

    if (options.hasOwnProperty('schema')) {
      Target = Target.schema(options.schema, options.schemaDelimiter);
    }

    if (!Array.isArray(instances)) {
      instance = instances;
      instances = undefined;
    }

    if (instances) {
      where[association.targetKey] = {
        [Op.in]: instances.map(instance => instance.get(association.foreignKey))
      };
    } else {
      if (association.targetKeyIsPrimary && !options.where) {
        return Target.findByPk(instance.get(association.foreignKey), options);
      } else {
        where[association.targetKey] = instance.get(association.foreignKey);
        options.limit = null;
      }
    }

    options.where = options.where ?
      {[Op.and]: [where, options.where]} :
      where;

    if (instances) {
      return Target.findAll(options).then(results => {
        const result = {};
        for (const instance of instances) {
          result[instance.get(association.foreignKey, {raw: true})] = null;
        }

        for (const instance of results) {
          result[instance.get(association.targetKey, {raw: true})] = instance;
        }

        return result;
      });
    }

    return Target.findOne(options);
  }

  /**
   * Set the associated model.
   *
   * @param {Model|String|Number} [newAssociation] An persisted instance or the primary key of an instance to associate with this. Pass `null` or `undefined` to remove the association.
   * @param {Object} [options] Options passed to `this.save`
   * @param {Boolean} [options.save=true] Skip saving this after setting the foreign key if false.
   * @return {Promise}
   */
  set(sourceInstance, associatedInstance, options) {
    const association = this;

    options = options || {};

    let value = associatedInstance;
    if (associatedInstance instanceof association.target) {
      value = associatedInstance[association.targetKey];
    }

    sourceInstance.set(association.foreignKey, value);

    if (options.save === false) return;

    options = _.extend({
      fields: [association.foreignKey],
      allowNull: [association.foreignKey],
      association: true
    }, options);

    // passes the changed field to save, so only that field get updated.
    return sourceInstance.save(options);
  }

  /**
   * Create a new instance of the associated model and associate it with this.
   *
   * @param {Object} [values]
   * @param {Object} [options] Options passed to `target.create` and setAssociation.
   * @see {@link Model#create}  for a full explanation of options
   * @return {Promise}
   */
  create(sourceInstance, values, fieldsOrOptions) {
    const association = this;

    const options = {};

    if ((fieldsOrOptions || {}).transaction instanceof Transaction) {
      options.transaction = fieldsOrOptions.transaction;
    }
    options.logging = (fieldsOrOptions || {}).logging;

    return association.target.create(values, fieldsOrOptions).then(newAssociatedObject =>
      sourceInstance[association.accessors.set](newAssociatedObject, options)
    );
  }
} called with something that's not a subclass of Sequelize.Model
    at Function.<anonymous> (E:...\Projects\WebApps\hr1\hr1\node_modules\sequelize\lib\associations\mixin.js:81:13)
    at Object.<anonymous> (E:...\Projects\WebApps\hr1\hr1\models\user_employee.js:22:14)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (E:...\Projects\WebApps\hr1\hr1\models\user.js:4:26)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (E:...\Projects\WebApps\hr1\hr1\routes\index.js:4:12)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)

这是我的代码

模型/User.js

var bcrypt =  require('bcrypt');
const sequelize = require('../config/connectionDatabase')
var Sequelize = require('sequelize');
const UserEmployee = require('../models/user_employee');

var User = sequelize.define('user_tm', {
    NameFirst: {
        type: Sequelize.STRING
    },
    NameLast: {
        type: Sequelize.STRING
    },
    username: {
        type: Sequelize.STRING,
        unique: true,
        allowNull: false
    },
    password: {
        type: Sequelize.STRING,
        allowNull: false
    }
}, {
    hooks: {
    beforeCreate: (user) => {
        const salt = bcrypt.genSaltSync();
        user.password = bcrypt.hashSync(user.password, salt);
    }
    },
    instanceMethods: {
    validPassword: function(password) {
        return bcrypt.compareSync(password, this.password);
    }
    }    
});

User.hasOne(UserEmployee, {foreignKey: 'UserID', as: 'User'});
User.prototype.validPassword = function (password) {
    return bcrypt.compareSync(password, this.password);
};
module.exports = User;

模型/user_employee.js

const sequelize = require('../config/connectionDatabase');
var Sequelize = require('sequelize');
const User = require('../models/user');

var UserEmployee = sequelize.define('user_employee_tm', {
    DateJoin: {
        type: Sequelize.DATE
    },
    UserID: {
        type: Sequelize.INTEGER,
        references: {
            model: User,
            key: "ID"
        }
    },
    CompanyID: {
        type: Sequelize.INTEGER
    }
});

// UserEmployee.hasOne(User, {as: 'User', foreignKey: 'UserID'});  
UserEmployee.belongsTo(User , {foreignKey: 'ID', as: 'Employee'});
module.exports = UserEmployee;

有什么我错过的吗?我尝试使用这个网址 https://dreamdevourer.com/example-of-sequelize-associations-in-feathersjs/

用于在模型中添加关联,但仍然存在同样的问题。

非常感谢您的帮助

【问题讨论】:

    标签: node.js express sequelize.js


    【解决方案1】:

    如果您同时添加多个关联,可能会打乱流程。在我的情况下,我删除了所有关联并一一添加它们,最终得到与之前产生此错误但现在工作正常的相同代码。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    我是 Sequelize 的新手。这种方法可能很幼稚,但它解决了我的简单用例中的鸡/蛋问题:

    models/Dog.js

    const {Model} = require('sequelize');
    
    module.exports = function (sequelize) {
      class Dog extends Model {}
    
      setTimeout(() => {
        Dog.hasMany(sequelize.models.Flea);
      }, 0);
    
      return Dog;
    }
    

    models/Flea.js

    const {Model} = require('sequelize');
    
    module.exports = function (sequelize) {
      class Flea extends Model {}
    
      setTimeout(() => {
        Flea.belongsTo(sequelize.models.Dog);
      }, 0);
    
      return Flea;
    }
    

    models/index.js:

    const sequelize = getConnectionSomehow();
    
    const Dog  = require('./Dog' )(sequelize);
    const Flea = require('./Flea')(sequelize);
    

    【讨论】:

      【解决方案3】:

      如果您有很多模型,将所有模型放在一个文件中是不可行的。

      对我有用的不是将模型添加到同一个文件中,而是创建一个新的 associations 文件并导入模型,然后在其中执行 belongsTo 等。然后在主应用程序文件中import './assocations'。这解决了错误。

      associations 文件大致如下:

      import {Post} from "./post";
      import {Tag} from "./tag";
      
      Tag.belongsToMany(Post, {
          through: 'PostTags'
      })
      
      Post.belongsToMany(Tag, {
          through: 'PostTags'
      })
      

      【讨论】:

      • “主应用程序文件”指的是什么?那是index.ts 模型文件吗?
      • 更新:对于那些对“主应用程序文件”感到困惑的人,我正在使用 Express.js 并将其导入到 src/index.ts 的顶部,如下所示:import './db/models/associations'; 似乎可以正常工作很好。
      【解决方案4】:

      这就是我解决它的方法:A.hasMany(models.B);在 A 模型上然后在 B 模型上 B.belongsTo(models.A);

      "use strict";
      const { Model } = require("sequelize");
      const quotation = require("./quotation");
      module.exports = (sequelize, DataTypes) => {
        class Clients 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
            Clients.hasOne(models.Quotation);
          }
        }
        Clients.init(
          {
            firstName: DataTypes.STRING,
            lastName: DataTypes.STRING,
            email: DataTypes.STRING,
            phone: DataTypes.STRING,
            contactName: DataTypes.STRING,
            contactPosition: DataTypes.STRING,
            rncCode: DataTypes.STRING,
            active: DataTypes.BOOLEAN,
          },
          {
            sequelize,
            modelName: "Clients",
          }
        );
      
        return Clients;
      };

      "use strict";
      const { Model } = require("sequelize");
      const quotation = require("./quotation");
      module.exports = (sequelize, DataTypes) => {
        class Clients 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
            Clients.hasOne(models.Quotation);
          }
        }
        Clients.init(
          {
            firstName: DataTypes.STRING,
            lastName: DataTypes.STRING,
            email: DataTypes.STRING,
            phone: DataTypes.STRING,
            contactName: DataTypes.STRING,
            contactPosition: DataTypes.STRING,
            rncCode: DataTypes.STRING,
            active: DataTypes.BOOLEAN,
          },
          {
            sequelize,
            modelName: "Clients",
          }
        );
      
        return Clients;
      };

      【讨论】:

        【解决方案5】:

        我发现我忘记从模型名称中删除 s 的问题

        User.belongsToMany(models.Roles, { 通过:“user_roles”, });

        【讨论】:

          【解决方案6】:

          推杆 A.hasOne(B)B.belongsTo(A) 在同一个文件中为我解决了这个问题。

          【讨论】:

          • 我可以确认这种方法有效......但这真的是个好主意吗?为此,我们必须 (1) 将关联(hasOnebelongsTo 等)与模型定义分开放在一个文件中,或者 (2) 将关联和模型定义放在一个文件中。
          • 如果您不想处理将所有模型都放在一个文件中的问题,请参阅我的回答,它几乎可以做到这一点,但更易于管理。
          【解决方案7】:

          对于未来的谷歌人来说,这也可能发生在看起来正确但存在循环依赖的代码中。在我的例子中,A 与 B 有一个 belongsTo 关系,当我在 B 处添加一个实例化 A 的钩子时,出现了错误(实际上,它出现在您添加 import 时)。

          我通过在 A 文件中添加钩子来修复它。

          【讨论】:

            【解决方案8】:

            您需要在名为associate(models) 的函数中添加关联。 models 参数包含所有现有的Model 定义,它们的定义名称(在本例中为“user_tm”)作为关键字。

            var User = sequelize.define('user_tm', {
              // ... user_tm definition
            });
            
            var UserEmployee = sequelize.define('user_employee_tm', {
              // ... user_employee_tm definition
            });
            
            UserEmployee.associate = (models) => {
              UserEmployee.belongsTo(models.user_tm, {foreignKey: 'ID', as: 'Employee'});
            };
            

            【讨论】:

            • 所以我需要将 UserEmployee 定义放在用户模型文件中?另外,我还有另一个警告 { SequelizeEagerLoadingError: user_tm is not associated to user_employee_tm!
            • 您还需要创建反向关系。
            • 我想补充一点,这个案例也很重要。确保定义名称与后面使用的完全相同。
            • associate 属性在 TypeScript 中不存在 :(
            【解决方案9】:

            原来我发现我只需要定义我的 UserEmployee 对象 这是我已经修复的代码

            const sequelize = require('../config/connectionDatabase');
            var Sequelize = require('sequelize');
            const User = require('../models/user');
            const Company = require('../models/company');
            
            var UserEmployee = sequelize.define('user_employee_tm', {
                DateJoin: {
                    type: Sequelize.DATE
                },
                UserID: {
                    type: Sequelize.INTEGER,
                    references: {
                        model: User,
                        key: "UserID"
                    }
                },
                CompanyID: {
                    type: Sequelize.INTEGER,
                    references: {
                        model: Company,
                        key: "CompanyID"
                    }
                }
            });
            UserEmployee.belongsTo(Company, {as: 'Company', foreignKey: 'CompanyID'});
            UserEmployee.belongsTo(User, {as: 'User', foreignKey: 'UserID'});
            module.exports = UserEmployee;
            

            不需要设置为关联,因为Sequelize已经设置方法关联它们,我也修复了它的关系。

            希望与我有同样问题的其他人可以照顾它,没有制作 2 个模型最终在 1 个文件中。

            附: 感谢 doublesharp 帮助指出我的错误做法

            【讨论】:

              猜你喜欢
              • 2018-04-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-04
              • 2020-09-21
              • 1970-01-01
              • 2020-12-20
              • 2020-09-26
              相关资源
              最近更新 更多