0

That is my "one" class

 'use strict';

var {subcategoria} = require('./');



const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Categoria 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
    }
  };
  Categoria.init({
    id: { type: DataTypes.NUMBER, 
      autoIncrement: true,
       primaryKey: true},
    nomeCategoria: { field: 'nome_categoria', type: DataTypes.STRING},
    nomeLinkCategoria:{ field: 'nome_link_categoria', type: DataTypes.STRING}
  }, {
    sequelize,
    tableName: 'categoria',
    modelName: 'Categoria',
  });

  Categoria.hasMany(subcategoria, {foreignKey: 'idCategoria', sourceKey: 'id'});

  return Categoria;
};

That's my "many" class

'use strict';



const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Subcategoria 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
    }
  };
  Subategoria.init({
    id: { type: DataTypes.NUMBER, 
      autoIncrement: true,
       primaryKey: true},
    idCategoria: DataTypes.NUMBER,
    nomeSubcategoria: DataTypes.STRING,
    nomeLinkSubcategoria: DataTypes.STRING
  }, {
    sequelize,
    tableName: 'subcategoria',

    modelName: 'Subcategoria'
  });
  return Subcategoria;
};

They are on the same folder... I am getting

 Error: Categoria.hasMany called with something that's not a subclass of Sequelize.Model
at Function.hasMany (c:\loja\lojag2\node_modules\.bin\node_modules\sequelize\dist\lib\associations\mixin.js:13:13)
at module.exports (c:\loja\lojag2\node_modules\.bin\models\categoria.js:33:13)
at c:\loja\lojag2\node_modules\.bin\models\index.js:24:54
at Array.forEach (<anonymous>)
at Object.<anonymous> (c:\loja\lojag2\node_modules\.bin\models\index.js:23:4)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
Diego Alves
  • 2,462
  • 3
  • 32
  • 65

1 Answers1

0

Indicate all associations in associate static methods and call them only after all models will be registered.

See my other answer to get the idea how to do it

Anatoly
  • 20,799
  • 3
  • 28
  • 42