Sequelizeを使用してMySQLデータベースにアクセスするnodeJSアプリケーションがあります。NodeJSでsequelizeメソッドにORDER BYを追加する方法
は、ユーザーのための2つのテーブルがあります:ユーザーが少なくとも一度は自分のパスワードを変更した場合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
行いません。何か...私はまだ最初の(最も古い)エントリを取得します。だから、
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
どのように私はfindUserWithUsername()
方法にORDER BY id
と同等のSQLを追加することができます....:ここではfindUserWithUsername()
方法から今すぐ実行のクエリですか?
http://stackoverflow.com/a/35451488/2956135 – Emma