I am using sequelize ORM in node js. I am join two table and get result but that result return with table name as prefix.
var Sequelize = require('sequelize');
var sequelize = new Sequelize('test', 'root', '', {
// configuration
}
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.role = require('./../model/definitions/role')(sequelize, Sequelize);
db.admin = require('./../model/definitions/admin')(sequelize, Sequelize);
db.admin.findAll({
include: [{
model: db.role,
where:{status : 'Active'},
}],
raw: true
}).then(function(result) {
console.log(result);
}).catch(function(error) {
console.log(error);
}).done();
Now I am getting this result:
[{
"id": 36,
"email": "[email protected]",
"username": "test",
"status": "Active",
"role.role_id": 1,
"role.role_name": "Admin"
}]
but I need this result:
[{
"id": 36,
"email": "[email protected]",
"username": "test",
"status": "Active",
"role_id": 1,
"role_name": "Admin"
}]
so, how to remove prefix table name 'role' from column. I have only need 'role_id' or 'role_name' not need this type data like 'role.role_id', 'role.role_name'
This should work,
db.admin.findAll({
attributes: ['id', 'username', 'email', 'status', 'role.role_id', 'role.role_name'],
include: [{
model: db.role,
where:{status : 'Active'},
attributes: []
}],
raw: true
})
I found this here
This should work
A
is a table that is associated to B
using a column called bId
so B has a primary key of Id
which is associates to a bid
column in A
table that is shown in the standard result set as b.id
but we want these two column to be called as Id & Name
A.belongsTo(B);
return await A.findAll({
attributes: [
[Sequelize.col('b.id'), 'Id'],
[Sequelize.col('b.name'), 'Name']
],
raw: true,
where: { /*Some condition*/ },
include: {
model: B,
attributes: [],
required: true
},
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With