我有一个nodeJS应用程序使用Sequelize访问MySQL数据库。
用户有两个表:user
和user_password
如果用户至少更改了一次密码,则user_password
表中的每个用户都有多个条目。我想通过findUserWithUsername(username)
验证用户,如下所示:
async findOne(query = {}, include = [] , attributes = ['id', 'username', 'email'] ) {
const user = await db.user.findOne({include: include, where: query, attributes: attributes});
return user;
}
async findUserWithUsername(username) {
const include = [
{
model: db.user_password,
attributes: ['id','algorithm', 'password', 'salt'],
order: 'id desc'
}
];
return await this.findOne({username: username}, include); // need to get last one since there could be multiples
}
这不起作用:order: 'id desc'
基本上,这些表已加入,并返回user_password
中的第一个密码条目,但order: id desc
没有做任何事情......我仍然得到第一个(最旧的)条目。这是现在从findUserWithUsername()
方法运行的查询:
SELECT `user`.`id`, `user`.`username`, `user`.`email`,
`user_password`.`id` AS `user_password.id`,
`user_password`.`algorithm` AS `user_password.algorithm`,
`user_password`.`password` AS `user_password.password`,
`user_password`.`salt` AS `user_password.salt`
FROM `user` AS `user`
LEFT OUTER JOIN `user_password` AS `user_password`
ON `user`.`id` = `user_password`.`user_id` WHERE `user`.`username` = 'joe' LIMIT 1
那么....如何将ORDER BY id
的sql等效项添加到findUserWithUsername()
方法?
答案 0 :(得分:1)
答案 1 :(得分:1)
也许你应该倒退:
<Composition>
<contained>
<Patient>
<id value="patient1"/>
.....
<Patient>
</contained>
<contained>
<MedicationOrder>
<id value="medicationOrder1"/>
</MedicationOrder>
</contained>
<contained>
<MedicationOrder>
<id value="medicationOrder2"/>
</MedicationOrder>
</contained>
<contained>
<List>
<id value="list-medicationOrder"/>
.....
<entry>
<item>
<reference value="#medicationOrder1"/>
</item>
</entry>
<entry>
<item>
<reference value="#medicationOrder2"/>
</item>
</entry>
</List>
</contained>
<section>
<title value="Section medication order list"/>
....
<entry>
<reference value="#list-medicationOrder"/>
</entry>
</section>
</Composition>
获取包含该用户名
的userPasswords也许您应该查看此问题seqeuelize issue 3173
答案 2 :(得分:1)
我认为您的代码语法存在一些问题。
order: [
['id', 'DESC']
]
应该做的伎俩。
文档:http://docs.sequelizejs.com/manual/tutorial/querying.html#ordering