Reputation: 69
I have the following problem, I have 2 Models: Client
module.exports = (sequelize, DataTypes) => {
const Client = sequelize.define('Client', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
phone: DataTypes.STRING,
mobile_phone: DataTypes.STRING,
email: DataTypes.STRING,
});
//creating association Client with Gender
Client.associate = function (models) {
Client.hasOne(models.Gender, {
//
});
};
return Client; }
and Gender:
module.exports = (sequelize, DataTypes) => {
const Gender = sequelize.define('Gender', {
name: DataTypes.STRING,
});
//creating association Gender with Employee
Gender.associate = function (models) {
models.Gender.hasMany(models.Employee, {
//
});
};
//creating association Gender with Client
Gender.associate = function (models) {
models.Gender.belongsTo(models.Client, {
foreignKey: 'gender_id'
});
};
return Gender; }
My ClientController:
const { Client, Gender } = require('../models');
class ClientController {
// Apresenta todos os resgistros do determinado Model
async index (req, res) {
const clients = await Client.findAll({});
return res.status(200).json({
status: 'ok',
clients
});
}
// Apresenta os atributos setados de um resgistro específico
async show (req, res) {
const client = await Client.findAll({
attributes: ['id', 'first_name', 'last_name', 'phone', 'mobile_phone', 'email'],
where: {
id: req.params.id
},
include: [{
model: Gender,
attributes: [],
}],
});
return res.status(200).json({
status: 'ok',
client
});
}
// Cria e salva um resgistro do Model especificado
async store (req, res) {
const client = await Client.create(req.body);
return res.status(201).json({
status: 'created',
client
});
}
// Edita e salva um resgistro do Model especificado
async update (req, res) {
const { first_name, last_name, phone, mobile_phone, email } = req.body;
const client = await Client.update({
first_name: first_name,
last_name: last_name,
phone: phone,
mobile_phone: mobile_phone,
email: email
}, {
where: {
id: req.params.id
}
});
return res.status(200).json({
status: 'updated',
client
});
}
// Exclui um resgistro do Model especificado
async destroy (req, res) {
await Client.destroy({
where: {
id: req.params.id
}
});
return res.status(204).json({
status: 'removed'
});
}
}
module.exports = new ClientController()
But the following returns to me:
node ./bin/www
api:server Listening on port 3000 +0ms (node:16328) UnhandledPromiseRejectionWarning: SequelizeEagerLoadingError: Gender is not associated to Client! at Function._getIncludedAssociation (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:715:13) at Function._validateIncludedElement (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:619:53) at options.include.options.include.map.include (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:516:37) at Array.map () at Function._validateIncludedElements (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:511:39) at Promise.try.then.then (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:1726:14) at tryCatcher (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\util.js:16:23) at Promise._settlePromiseFromHandler (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:547:31) at Promise._settlePromise (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:604:18) at Promise._settlePromise0 (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:649:10) at Promise._settlePromises (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:729:18) at _drainQueueStep (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:93:12) at _drainQueue (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:86:9) at Async._drainQueues (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:102:5) at Immediate.Async.drainQueues [as _onImmediate] (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:15:14) at runCallback (timers.js:705:18) at tryOnImmediate (timers.js:676:5) at processImmediate (timers.js:658:5) (node:16328) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:16328) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Obs: In n: m relationships with pivot Table, everything is normal, could someone help me?
Upvotes: 1
Views: 2805
Reputation: 22758
You're overwriting your first association with the second one:
//creating association Gender with Employee
Gender.associate = function (models) {
models.Gender.hasMany(models.Employee, {
//
});
};
//creating association Gender with Client
Gender.associate = function (models) {
models.Gender.belongsTo(models.Client, {
foreignKey: 'gender_id'
});
};
just combine them into a single one:
//creating Gender associations
Gender.associate = function (models) {
models.Gender.hasMany(models.Employee, {
models.Gender.belongsTo(models.Client, {
foreignKey: 'gender_id'
});
};
Upvotes: 1