sequelize problem with relays

Asked

Viewed 113 times

0

Hello, I have some tables in mysql, I created the models using sequelize-auto and associations manually. The problem is that when I try to include these relationships I come across the error SequelizeEagerLoadingError: tbl_contato_visitas is not associated to tbl_visitas! the structure of the tables is 1 for n, or a visit can have n contact and a contact only one visit.

table visits

module.exports = function (sequelize, DataTypes) {
  return sequelize.define('tbl_visitas', {
    id_visitas: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    km: {
      type: DataTypes.STRING(45),
      allowNull: true
    },
    km_total: {
      type: DataTypes.STRING(45),
      allowNull: true
    },
    deslocamento: {
      type: DataTypes.STRING(45),
      allowNull: true
    },
    saida: {
      type: DataTypes.STRING(45),
      allowNull: true
    },
    retorno: {
      type: DataTypes.STRING(45),
      allowNull: true
    }
  }, {
    tableName: 'tbl_visitas',
    classMethods: {
      associate: function (models) {
        models.tbl_visitas.belongsToMany(models.tbl_contato_visitas, );
      }
    }
  });
};

Table contact

/* jshint indent: 2 */

module.exports = function (sequelize, DataTypes) {
  return sequelize.define('tbl_contato_visitas', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    id_visitas: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      references: {
        model: 'tbl_visitas',
        key: 'id_visitas'
      }
    },
    nome: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    telefone_1: {
      type: DataTypes.STRING(30),
      allowNull: true
    },
    telefone_2: {
      type: DataTypes.STRING(30),
      allowNull: true
    },
    email: {
      type: DataTypes.STRING(50),
      allowNull: true
    }
  }, {
    tableName: 'tbl_contato_visitas',
    classMethods: {
      associate: function (models) {
        models.tbl_contato_visitas.belongsTo(models.tbl_visitas, {
          foreignKey: 'id_visitas',
          targetKey: 'id_visitas'
        });
      }
    }


  });
};

where I try to use

const express = require('express'),
    router = express.Router();
const {
    tbl_visitas,
    tbl_locais_visitas,
    tbl_contato_visitas
} = require('../../models');


router.get('/:id', (req, res) => {
    tbl_visitas.findByPk(req.params.id, {
        include: [tbl_contato_visitas]
    }).then((result) => {
        res.json(result)
    }).catch((err) => {
        res.json(String(err))
    });
});

module.exports = router

1 answer

0


I solved by adding the relations in the file I do the searches.

tbl_visitas.hasMany(tbl_contato_visitas, {
    foreignKey: 'id_visitas',
    // targetKey: 'id_visitas'
});
tbl_contato_visitas.belongsTo(tbl_visitas, {
    foreignKey: 'id_visitas',
    targetKey: 'id_visitas'
});

Browser other questions tagged

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