데이터 이후로 테이블 이름으로 연도와 같은 정보를 절대로 저장하지 마십시오. 실제 사용할 수있는 데이터와 같이 별도의 테이블 항목으로 저장해야합니다.
변경 company_historical_<year>
은 company_historical
단지 회사 역사 항목이 가질 수있는 가능한 모든 년을 가지고 company_historical_years
라는 새 테이블을 만들 수 있습니다.
그런 다음 회사 역사 항목과 관련 회사 Historcal 년 항목 간의 관계를 만들 수 있습니다.
그래서 같은 :
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);
}
}
};
한 다음 그것을 조회 할 수 있습니다
CompanyHistoricalYears.findOne({
where: {
year: 2011, // or you can use "new Date().getFullYear()"
},
include: [CompanyHistorical]
});
이 당신에게 그 해 내에있는 하나의 CompanyHistoricalYears
항목 모든 CompanyHistorical
항목을 제공 할 것입니다.
이 내용 중 이해가되지 않는 부분이 있으면 언제든지 문의하십시오.