Change Model's Table Name Runtime
I am using Sequelize.js and need to save historical data till specific year and want to separate my tables by year's prefix for example prices_2010, prices_2011 e.g. I can create s
Solution 1:
You should never store any kind of information like years as table names since its data. You should store them as separate table entries, as actual usable data.
Change company_historical_<year>
to just company_historical
and create a new table called company_historical_years
which just has all the possible years the Company Historical entries can have.
Then create a relationship between the Company Historical entry and the related Company Historcal Years entry.
So something like:
var CompanyHistoricalYears = sequelize.define('company_historical_years', {
year: {
type: Sequelize.INTEGER
},
classMethods: {
associate: function (models) {
CompanyHistoricalYears.hasMany(models.CompanyHistorical);
}
}
};
var CompanyHistorical = sequelize.define('company_historical', {
...
classMethods: {
associate: function (models) {
CompanyHistorical.belongsTo(models.CompanyHistoricalYears);
}
}
};
and then you can query it with:
CompanyHistoricalYears.findOne({
where: {
year: 2011, // or you can use "new Date().getFullYear()"
},
include: [CompanyHistorical]
});
this will give you a single CompanyHistoricalYears
entry and all the CompanyHistorical
entries that are within that year.
If non of this makes sense then feel free to comment with any questions.
Post a Comment for "Change Model's Table Name Runtime"