Thursday, 17 October 2019

Help with Sequelize ORM

I didn't see any rules in the sidebar and i'm about to rip my hair out. I'm a student working on a project using Sequelize as my ORM in a node.js app. Background is that i've got a dataset of 27,000 names with some other columns for things like sex and ethnicity. I'm trying to query them based on user input, which is working fine. A basic query of WHERE for sex and ethnicity work fine, and but I need to be able to query for names that start with a chosen letter. The sequelize documentation isn't the most verbosely written doc out there... But i've taken that i should use iLike in this case. I'm taking in the letter in the body of a request, and it spits out a one letter string just as intended. My app is set up in the MVC paradigm, and all of this is happening in my controller file under a route. If no one can help, or if this is against the rules, please point me in the right direction as I'm ripping my hair out over this one.The string im using below if anyone cares: var startingLetter = req.body.startingLetter; What works:db.BabyName.findAll({ limit: resultNum, where: { sex: genderChoice, ethnicity: ethnicityChoice, } }) What i've tried that does not work:db.BabyName.findAll({ limit: resultNum, where: { sex: genderChoice, ethnicity: ethnicityChoice, name: { [db.Sequelize.Op.iLike]: startingLetter + "%"} } }) This gives me an error in SQL syntax that looks like thisExecuting (default): SELECT `id`, `Year_of_birth`, `sex`, `ethnicity`, `name`, `occurences`, `name_rank`, `createdAt`, `updatedAt` FROM `BabyNames` AS `BabyName` WHERE `BabyName`.`sex` = 'male' AND `BabyName`.`ethnicity` = 'white' AND `BabyName`.`name` ILIKE 'A%'; Unhandled rejection SequelizeDatabaseError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ILIKE 'A%'' at line 1 anddb.BabyName.findAll({ limit: resultNum, where: { sex: genderChoice, ethnicity: ethnicityChoice, name: { $iLike: startingLetter + "%"} } }) which gives me (because I am not using whatever library has this syntax, i'd imagine)Unhandled rejection Error: Invalid value { '$iLike': 'A%' }

Submitted October 18, 2019 at 04:47AM by camelCaseCoding

No comments:

Post a Comment