2017-02-20 478 views
0

我想使用sequelize.js查詢模型以查找包含約束的記錄。我怎麼做?Sequelize:查找所有匹配的內容(不區分大小寫)

這就是我現在所擁有的:

Assets 
    .findAll({ limit: 10, where: ["asset_name like ?", '%' + request.body.query + '%'] }) 
    .then(function(assets){ 
    return response.json({ 
     msg: 'search results', 
     assets: assets 
    }); 
    }) 
    .catch(function(error){ 
    console.log(error); 
    }); 

,但我得到了以下錯誤:

{ error: operator does not exist: character varying @> unknown 
     at Connection.parseE (/home/travellr/safe-star.com/SafeStar/node_modules/pg/lib/connection.js:554:11) 
     at Connection.parseMessage (/home/travellr/safe-star.com/SafeStar/node_modules/pg/lib/connection.js:381:17) 
     at Socket.<anonymous> (/home/travellr/safe-star.com/SafeStar/node_modules/pg/lib/connection.js:117:22) 
     at emitOne (events.js:96:13) 
     at Socket.emit (events.js:188:7) 
     at readableAddChunk (_stream_readable.js:176:18) 
     at Socket.Readable.push (_stream_readable.js:134:10) 
     at TCP.onread (net.js:548:20) 
    name: 'error', 
    length: 209, 
    severity: 'ERROR', 
    code: '42883', 
    detail: undefined, 
    hint: 'No operator matches the given name and argument type(s). You might need to add explicit type casts.', 
    position: '246', 
    internalPosition: undefined, 
    internalQuery: undefined, 
    where: undefined, 
    schema: undefined, 
    table: undefined, 
    column: undefined, 
    dataType: undefined, 
    constraint: undefined, 
    file: 'parse_oper.c', 
    line: '722', 
    routine: 'op_error', 
    sql: 'SELECT "id", "asset_name", "asset_code", "asset_icon", "asset_background", "asset_add_view", "asset_add_script", "asset_add_id_regex", "date_created", "uniqueValue", "createdAt", "updatedAt" FROM "assets" AS "assets" WHERE "assets"."asset_name" @> \'%a%\' LIMIT 10;' }, 
    sql: 'SELECT "id", "asset_name", "asset_code", "asset_icon", "asset_background", "asset_add_view", "asset_add_script", "asset_add_id_regex", "date_created", "uniqueValue", "createdAt", "updatedAt" FROM "assets" AS "assets" WHERE "assets"."asset_name" @> \'%a%\' LIMIT 10;' } 

如何使用一個包含sequelize查詢?

回答

0
Assets.findAll({ 
     limit: 10, 
     where: { 
      asset_name: { 
       $like: '%' + request.body.query + '%' 
      } 
     } 
}).then(function(assets){ 
    return response.json({ 
     msg: 'search results', 
     assets: assets 
    }); 
}).catch(function(error){ 
    console.log(error); 
}); 

編輯

爲了使其不區分大小寫,您可以使用SQL LOWER功能,但以前你也不得不降低情況下,你request.body.query值。然後Sequelize查詢將看起來像

let lookupValue = request.body.query.toLowerCase(); 

Assets.findAll({ 
    limit: 10, 
    where: { 
     asset_name: sequelize.where(sequelize.fn('LOWER', sequelize.col('asset_name')), 'LIKE', '%' + lookupValue + '%') 
    } 
}).then(function(assets){ 
    return response.json({ 
     msg: 'message', 
     assets: assets 
    }); 
}).catch(function(error){ 
    console.log(error); 
}); 

它的作用是降低的情況下,從表中你asset_name值,以及較低的情況下,request.body.query值。在這種情況下,您會比較兩個較低的套管字符串。

爲了更好地理解在這種情況下發生了什麼,我建議您查看關於sequelize.where(),sequelize.fn()以及sequelize.col()的續集文檔。這些功能在嘗試執行一些不尋常的查詢時非常有用,而不是簡單的findAllfindOne

在這種情況下sequelize當然是你的Sequelize實例。

+0

謝謝!!!有沒有辦法做到不區分大小寫? A ++ – ryanwaite28

+0

我已經更新了答案,現在應該不區分大小寫。 – piotrbienias

+0

如果您使用的是PG,您可以使用''[Op.iLike]:\'%$ {request.body.query}%\'',然後跳過低級的cruft。 – Scott