Sequelize error when performing BD registration [Model.hasMany called with Something that’s not a subclass of Sequelize.Model]

Asked

Viewed 360 times

-2

I’m doing a Ode project, using sequelize as ORM. I have 4 tables relating [Factor, Subfactor, Obs_factor, Goals], when I will run the create of this error: Error: Factor.hasMany called with Something that’s not a subclass of Sequelize.Model

Remarks:

database: postgresql

An obs_factor can only be related to one factor;

A subfactor has a factor;

A factor may be related to several sub_factors;

A goal relates to a subfactor;

model Fator

    'use strict';

    module.exports = (sequelize, DataTypes) => {
      const Fator = sequelize.define('Fator', {
        descricao: DataTypes.STRING
      }, {});
      Fator.associate = function(models) {
        this.belongsTo(models.Obs_fator, {
          as: 'obs_Fatores'
        });
        this.hasMany(models.Sub_fator, {
          foreignKey: 'id',
          as: 'Fatores'
        })
      };
      return Fator;
    };

model Sub_factor

    'use strict';
    const Metas = require('./metas');

    module.exports = (sequelize, DataTypes) => {
      const Sub_Fator = sequelize.define('Sub_Fator', {
        fatorId: DataTypes.INTEGER,
        descricao: DataTypes.STRING
      }, {});
      Sub_Fator.associate = function(models) {
        Sub_Fator.belongsTo(models.Fator, {
          foreignKey: 'fatorId',
          as: 'Fator'
        });
        Sub_Fator.hasMany(Metas);
      };
      return Sub_Fator;
    };

model obs_factor

    'use strict';
    module.exports = (sequelize, DataTypes) => {
      const Obs_fator = sequelize.define('Obs_fator', {
        alunoId: DataTypes.INTEGER,
        fatoId: DataTypes.INTEGER,
        descricao: DataTypes.STRING,
        value: DataTypes.INTEGER
      }, {});
      Obs_fator.associate = function(models) {
        Obs_fator.belongsTo(models.Fator, {
          foreignKey:'fatoId', as:'fator'
        })
      };
      return Obs_fator;
    };

model Metas

    'use strict';
    module.exports = (sequelize, DataTypes) => {
      const Metas = sequelize.define('Metas', {
        sub_fatorId: DataTypes.INTEGER,
        alunoId: DataTypes.INTEGER,
        descricao: DataTypes.STRING,
        value: DataTypes.INTEGER
      }, {});
      Metas.associate = function(models) {
        Metas.belongsTo(models.Sub_fator, {
          foreignKey: 'sub_fatorId', as: 'sub_fator'
        })
      };
      return Metas;
    };

1 answer

0


Your problem occurs due to a typo.

In the Model Factor, use this.hasMany(models.Sub_Fator, { instead of this.hasMany(models.Sub_fator, {. Emphasis on Subfactor instead of Subfactor.

This is because in Model Sub_factor you are exporting defining as:

  const Sub_Fator = sequelize.define('Sub_Fator', {

(With the capital F).

Another option for the correction would be to rename Model Sub_factor for the tiny F.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.