Hi,
Let's say I have two legacy tables: one for Reports
var reportData = sequelize.define('reportData', {
Id: {
type: Sequelize.INTEGER,
primaryKey: true,
allowNull: false
ConceptId: {
type: Sequelize.INTEGER,
field: 'ConceptId',
referencesKey: 'ConceptId'
},
ReportPeriodId: {
type: Sequelize.INTEGER,
field: 'ReportPeriodId',
referencesKey: 'ReportPeriodId'
},
FiscalYear: Sequelize.INTEGER,
Value: Sequelize.DECIMAL,
CreateOn: Sequelize.DATE,
});
and one for Periods
var reportPeriod = sequelize.define('ReportPeriod', {
Id: {
type: Sequelize.INTEGER,
primaryKey: true,
allowNull: false
},
Name: {
type: Sequelize.STRING(50)
},
Description: {
type: Sequelize.STRING(200)
},
}, {
schema: 'Example',
tableName: 'DimReportPeriod',
freezeTableName: true,
timestamps: false
});
I want to retrieve all the data from the reports with just one attribute from the included model.
I have done the following association (not sure if this is right):
reportPeriod.hasOne(reportData, {
foreignKey: 'ReportPeriodId'
});
reportData.belongsTo(reportPeriod)
and my query is the following:
.sync()
.then(function() {
return reportData.findAll({
include: [{model: reportPeriod, attributes: ['name']} ],
})
.then(function(content) {
console.log(JSON.stringify(content))
})
.catch(function(error) {
console.log('Problem trying to retrieve data: ', error);
});
The result of my query is the following:
[{"Id":23731063,"ConceptId":17807,"ReportPeriodId":1,"FiscalYear":2014,"Value":14230480,"CreateOn":"2015-03-31T23:22:22.920Z","ReportPeriod":{"name":"FY"}},
{"Id":23731064,"ConceptId":17807,"ReportPeriodId":8,"FiscalYear":2014,"Value":14230480,"CreateOn":"2015-03-31T23:22:22.920Z","ReportPeriod":{"name":"Q4_3M"}}]
There are 2 issues:
"ReportPeriod":{"name":"FY"}
have
"ReportPeriod":"FY"
Thanks
I am using Sequelize 2.0.5
You can use include.through.attributes
to limit the attributes on the through table.
As for converting an object to a value, that's a javascript question more than sequelize, i suggest using .map
Thanks @mickhansen , is there documentation on inclde.through.attributes
?
@Bondifrench Were you able to get the solution for
and how can I get instead of an object, the value of the attribute ie instead of
How to use .map function in the query
Most helpful comment
Thanks @mickhansen , is there documentation on
inclde.through.attributes
?