Reputation: 67
I'm having a problem with sequelize. I need to use a JOIN, but I can't do the relationships. This are my models (User is shrunk, it's really big and the only field I need for this is NR_UTILIZADOR):
var UtilizadoresModel = sequelize.define('UTILIZADORES', {
NR_UTILIZADOR: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
}
}, {
freezeTableName: true,
paranoid: true,
timestamps: true,
hooks: {
beforeCreate: (user, options) => {
return bcrypt.hash(user.PASSWORD, 10)
.then(hash => {
user.PASSWORD = hash
})
.catch(err => {
throw new Error()
})
},
},
})
var Validacoes = sequelize.define('VALIDACOES', {
NR_VALIDACAO: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
NR_VALIDADO: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: Utilizadores,
key: 'NR_UTILIZADOR'
}
},
NR_VALIDADOR: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: Utilizadores,
key: 'NR_UTILIZADOR'
}
},
DATA_HORA_VALIDACAO: {
type: Sequelize.DATE,
allowNull: false
},
ESTADO: {
type: Sequelize.ENUM('APROVADO', 'DESAPROVADO'),
allowNull: false
}
}, {
freezeTableName: true,
timestamps: false,
})
I'm doing this:
User.hasMany(Validacoes, {
foreignKey: 'NR_UTILIZADOR'
})
Validacoes.belongsTo(User)
I need to associate NR_VALIDADO and NR_VALIDADOR from Validacoes to NR_UTILIZADOR from Utilizadores.
When I try to execute a findAll with include I get this:
await User.findAll({
attributes: [
'NR_UTILIZADOR',
'NOME_UTILIZADOR',
'FREGUESIA',
'createdAt',
'NR_VALIDADOR',
],
where: {
VALIDADO: false,
deletedAt: {
[Op.ne]: null
},
ESTADO: 'DESAPROVADO'
},
order: [
['createdAt', 'ASC'],
],
include: [{
model: Validacoes
}],
paranoid: false
})
original: error: column VALIDACOES.NR_UTILIZADOR does not exist
Thank you all for your time
Upvotes: 0
Views: 1605
Reputation: 22803
According to your model definitions the NR_UTILIZADOR
column is not in Validacoes
as you try to declare by
User.hasMany(Validacoes, {
foreignKey: 'NR_UTILIZADOR'
})
This association definition says that Validacoes
has NR_UTILIZADOR
that is linked to User
.
I suppose you should define associations between Validacoes
and User
like this:
// you should add an alias to each association
// to distinguish two links to User from each other
// as well as two Validacoes links.
Validacoes.belongsTo(User, { foreignKey: 'NR_VALIDADO', as: 'Validado' })
Validacoes.belongsTo(User, { foreignKey: 'NR_VALIDADOR', as: 'Validador' })
User.hasMany(Validacoes, { foreignKey: 'NR_VALIDADO', as: 'ValidadoValidacoes' })
User.hasMany(Validacoes, { foreignKey: 'NR_VALIDADOR', as: 'ValidadorValidacoes' })
And FINALLY your Sequelize query will look something like this:
await User.findAll({
attributes: [
'NR_UTILIZADOR',
'NOME_UTILIZADOR',
'FREGUESIA',
'createdAt',
'NR_VALIDADOR', // this column does not exist in User model
],
where: {
VALIDADO: false,
deletedAt: {
[Op.ne]: null
},
ESTADO: 'DESAPROVADO'
},
order: [
['createdAt', 'ASC'],
],
include: [{
model: Validacoes,
as: 'ValidadoValidacoes'
}, {
model: Validacoes,
as: 'ValidadorValidacoes'
}],
paranoid: false
})
Upvotes: 1