gpt4 book ai didi

node.js - Sequelize 生成错误的查询? Sequelize 自动 ts 和 Sequelize

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:04 28 4
gpt4 key购买 nike

我正在使用 Typescript、NodeJS 和sequelize-auto-ts 进行模型生成。我想要放置包含查找查询的两个模型给了我错误的结果,这又是由于触发了错误的查询。这是有问题的两个模型;

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});
}
}
});

我正在进行的数据库调用如下;

 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,我必须使用 ownTo 并且它起作用了。供引用文档 http://sequelizejs.com/docs/latest给了我一些感觉,但过了一会儿 gist.github.com/sdepold/1757695

关于node.js - Sequelize 生成错误的查询? Sequelize 自动 ts 和 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25580177/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com