我正在使用Typescript,NodeJS和sequelize-auto-ts进行模型生成。我想在其中放置包含find查询的两个模型给出了错误的结果,而这又是由于错误的查询被触发。这是有问题的两个模型;
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;
请帮助我指出我做错了什么或错过了什么。
答案 0 :(得分:2)
所以我发现我的错误而不是hasOne我必须使用belongsTo并且它工作。参考文档http://sequelizejs.com/docs/latest给了我一些感觉但是过了一段时间gist.github.com/sdepold/1757695