koajs / koa

Expressive middleware for node.js using ES2017 async functions
https://koajs.com
MIT License
35.2k stars 3.23k forks source link

[fix] Some things cannot be found in the database where condition search, unless the where condition search ID #1763

Closed wintsa123 closed 1 year ago

wintsa123 commented 1 year ago

Code to reproduce

async findByQiniuKey(qiniu_key: string) { const result = await qiniuDataModel.findAndCountAll({ where: { qiniu_key }, }); return result; } const model = sequelize.define<QiniuDataModel>( 'qiniu_data', { id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false, autoIncrement: true, }, user_id: { type: DataTypes.INTEGER, }, prefix: { type: DataTypes.STRING(50), }, bucket: { type: DataTypes.STRING(50), }, qiniu_key: { type: DataTypes.STRING(150), }, qiniu_hash: { type: DataTypes.STRING(50), }, qiniu_fsize: { type: DataTypes.STRING(50), }, qiniu_mimeType: { type: DataTypes.STRING(50), }, qiniu_putTime: { // 会返回:16511776862952760,超出DataTypes.INTEGER大小,可以使用DataTypes.BIGINT type: DataTypes.STRING(50), }, qiniu_type: { type: DataTypes.STRING(50), }, qiniu_status: { type: DataTypes.STRING(50), }, qiniu_md5: { type: DataTypes.STRING(50), }, }, { paranoid: true, freezeTableName: true, createdAt: 'created_at', updatedAt: 'updated_at', deletedAt: 'deleted_at', } ); cant find.....but,use where id=something,it can find Executing (default): SELECTid,user_id,prefix,bucket,qiniu_key,qiniu_hash,qiniu_fsize,qiniu_mimeType,qiniu_putTime,qiniu_type,qiniu_status,qiniu_md5,created_at,updated_at,deleted_atFROMqiniu_dataASqiniu_dataWHERE (qiniu_data.deleted_atIS NULL ANDqiniu_data.qiniu_key= 'homeImage/111.png'); result:{ count: 0, rows: [] }

but use sqltool,it can work! image

wintsa123 commented 1 year ago

image image image

wintsa123 commented 1 year ago

koa2 nodejs lastvision

diego-oikos commented 1 year ago

I don't think that has anything to do with Koa... You should check the SQL generated by Sequelize to perform that database query, as that can shed some light into why your code isn't working as expected. Add logging: console.log, like this:

async findByQiniuKey(qiniu_key: string) {
    const result = await qiniuDataModel.findAndCountAll({
        logging: console.log,
        where: { qiniu_key },
    });
    return result;
}

to see the generated SQL, and run that on you SQLTool to see what is going on.