我正在使用Typescript,NodeJS和sequelize-auto-ts進行模型生成。我想放置一個包含find查詢的兩個模型給了我錯誤的結果,這反過來又是由於錯誤的查詢被解僱了。這裏是兩個有問題的模型;Sequelize生成錯誤的查詢? sequelize-auto-ts和sequelize
rewardcodes = <types.rewardcodesModel> sequelize.define<types.rewardcodeInstance, types.rewardcodePojo>('rewardcodes', {
'rewardid': { type: 'number', primaryKey: true, autoIncrement: true },
'code':'string',
'amount':'string'
},
{
timestamps: false,
classMethods: {
getrewardcode:(rewardcode:types.rewardcodePojo) => {
var where:{[key:string]:any} = {};
if (rewardcode['rewardid'] !== undefined) { where['rewardid'] = rewardcode['rewardid']}
if (rewardcode['code'] !== undefined) { where['code'] = rewardcode['code']}
if (rewardcode['amount'] !== undefined) { where['amount'] = rewardcode['amount']}
return this.find({where: where});
}
}
});
和
userfreecredits = <types.userfreecreditsModel> sequelize.define<types.userfreecreditInstance, types.userfreecreditPojo>('userfreecredits', {
'id': { type: 'number', primaryKey: true, autoIncrement: true },
'userid': { type: 'number', references:user },
'rewardid': {type: 'number',references: rewardcodes},
'isexpired':'Buffer',
'isused':'Buffer',
'isactive':'Buffer',
'expireydate':'Date'
},
{
timestamps: false,
classMethods: {
getuserfreecredit:(userfreecredit:types.userfreecreditPojo) => {
var where:{[key:string]:any} = {};
if (userfreecredit['id'] !== undefined) { where['id'] = userfreecredit['id']}
if (userfreecredit['userid'] !== undefined) { where['userid'] = userfreecredit['userid']}
if (userfreecredit['rewardid'] !== undefined) { where['rewardid'] = userfreecredit['rewardid']}
if (userfreecredit['isexpired'] !== undefined) { where['isexpired'] = userfreecredit['isexpired']}
if (userfreecredit['isused'] !== undefined) { where['isused'] = userfreecredit['isused']}
if (userfreecredit['isactive'] !== undefined) { where['isactive'] = userfreecredit['isactive']}
if (userfreecredit['expireydate'] !== undefined) { where['expireydate'] = userfreecredit['expireydate']}
return this.find({where: where});
}
}
});
和DB調用我提出如下;
var association = <sequelize.AssociationOptions>{};
association.foreignKey = "rewardid";
models.userfreecredits.hasOne(models.rewardcodes,association);
models.rewardcodes.hasMany(models.userfreecredits);
var findOptions = <sequelize.FindOptions>{};
findOptions.where = { userid: userId, isexpired: false, isused: false, isactive: true, expireydate: { gte: new Date() } };
findOptions.include = models.rewardcodes;
return models.userfreecredits.find(findOptions).complete((err: Error, credits: types.userfreecreditInstance) => {
return credits;
});
但是從上面生成的查詢是錯誤的,並表示userfreecredit.id = rewardcodes.id
SELECT `userfreecredits`.*, `rewardcode`.`rewardid` AS `rewardcode.rewardid`,
`rewardcode`.`code` AS `rewardcode.code`, `rewardcode`.`amount` AS
`rewardcode.amount` FROM `userfreecredits` LEFT OUTER JOIN `rewardcodes` AS
`rewardcode` ON `userfreecredits`.`id` = `rewardcode`.`rewardid`
WHERE `userfreecredits`.`userid`='1' AND `userfreecredits`.`isexpired`=false
AND `userfreecredits`.`isused`=false AND `userfreecredits`.`isactive`=true
AND`userfreecredits`.`expireydate` >= '2014-08-29 21:49:08' LIMIT 1;
請幫我指出我在做什麼錯誤或丟失。
所以我找到了我錯誤,而不是hasOne我不得不使用belongsTo它的工作,以供參考文檔給我一些道理,但一段時間後https://gist.github.com/sdepold/1757695 – Bilal 2014-08-30 10:27:20
自己創建答案並接受它,然後可以看到從問題列表中關閉該主題(並回答)... – 2014-09-02 11:40:34