Sequelize - 查看与您的 MySQL 服务器版本相对应的手册,了解要使用的正确语法
Sequelize - Check the manual that corresponds to your MySQL server version for the right syntax to use
我正在尝试部署一个网络应用程序,数据库是一个 MySQL 我通过 Sequelize 连接到的数据库。
通常我使用 PostgreSQL,但虚拟主机使用 MySQL 并通过 phpMyAdmin 访问。
我有一个 api & 数据库查询,可以在本地和 Heroku 中使用(我假设是因为两者都使用 PostgreSQL),但不适用于此虚拟主机,可能是因为它们使用 MySQL.
当我进行 API 调用时,出现以下错误。
"sqlMessage": "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 '%%' OR `Cocktail`.`description` ILIKE '%')' at line 1",
"sql": "SELECT count(*) AS `count` FROM `Cocktails` AS `Cocktail` WHERE (`Cocktail`.`name` ILIKE '%%' OR `Cocktail`.`description` ILIKE '%');"
下面是我正在做的查询。这似乎只有在我删除 iLike
、模数运算符 %
和 Op.or
时才有效;这基本上剥离了搜索功能。
我最初安装了 mysql2
,但我还添加了 mysql
,希望它能以某种方式解决这个问题;但它没有。
我该如何解决这个问题?对不起,如果是一个小问题..我真的不知道如何进行..
const paginator = async (req, res, limit) => {
const { searchIngredients, searchTerm } = req.query;
const ingredients =
searchIngredients &&
searchIngredients.split(',').map((ingredient) => `%${ingredient}%`);
const { page } = req.query;
if (!searchIngredients) {
await Cocktail.findAndCountAll({
where: {
[Op.or]: [
{
name: {
[Op.iLike]: `%${searchTerm || ''}%`,
},
},
{
description: {
[Op.iLike]: `%${searchTerm || ''}%`,
},
},
],
},
}).then(async (data) => {
const offset = limit * (page === undefined ? 1 - 1 : page - 1);
await Cocktail.findAll({
limit,
offset,
order: [[`createdAt`, 'DESC']],
where: {
[Op.or]: [
{
name: {
[Op.iLike]: `%${searchTerm || ''}%`,
},
},
{
description: {
[Op.iLike]: `%${searchTerm || ''}%`,
},
},
],
},
include: [
{
model: Ingredient,
as: 'ingredients',
attributes: {
exclude: ['createdAt', 'updatedAt', 'Cocktail_Ingredient'],
},
},
{
model: Step,
as: 'steps',
attributes: {
exclude: ['createdAt', 'updatedAt', 'Cocktail_Step'],
},
},
],
})
.then((cocktails) => {
return res.status(200).send({ count: data.count, limit, cocktails });
})
.catch(() => res.status(500).send({ message: 'error here too' }));
});
编辑:
使用以下方法将我的 iLike
查询转换为 MySQL 友好:
Sequelize.where(Sequelize.fn('lower', Sequelize.col('name')), {
[Op.like]: `%${searchTerm || ''}%`,
}),
只有LIKE,没有ILIKE
。
Case sensitivity 的内容有所不同。
代替 I LIKE,您可以使用 LOWER() 和 LIKE 来忽略区分大小写。
示例:SELECT * FROM products WHERE LOWER(productName) LIKE LOWER('%my search terms%');
我正在尝试部署一个网络应用程序,数据库是一个 MySQL 我通过 Sequelize 连接到的数据库。
通常我使用 PostgreSQL,但虚拟主机使用 MySQL 并通过 phpMyAdmin 访问。
我有一个 api & 数据库查询,可以在本地和 Heroku 中使用(我假设是因为两者都使用 PostgreSQL),但不适用于此虚拟主机,可能是因为它们使用 MySQL.
当我进行 API 调用时,出现以下错误。
"sqlMessage": "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 '%%' OR `Cocktail`.`description` ILIKE '%')' at line 1",
"sql": "SELECT count(*) AS `count` FROM `Cocktails` AS `Cocktail` WHERE (`Cocktail`.`name` ILIKE '%%' OR `Cocktail`.`description` ILIKE '%');"
下面是我正在做的查询。这似乎只有在我删除 iLike
、模数运算符 %
和 Op.or
时才有效;这基本上剥离了搜索功能。
我最初安装了 mysql2
,但我还添加了 mysql
,希望它能以某种方式解决这个问题;但它没有。
我该如何解决这个问题?对不起,如果是一个小问题..我真的不知道如何进行..
const paginator = async (req, res, limit) => {
const { searchIngredients, searchTerm } = req.query;
const ingredients =
searchIngredients &&
searchIngredients.split(',').map((ingredient) => `%${ingredient}%`);
const { page } = req.query;
if (!searchIngredients) {
await Cocktail.findAndCountAll({
where: {
[Op.or]: [
{
name: {
[Op.iLike]: `%${searchTerm || ''}%`,
},
},
{
description: {
[Op.iLike]: `%${searchTerm || ''}%`,
},
},
],
},
}).then(async (data) => {
const offset = limit * (page === undefined ? 1 - 1 : page - 1);
await Cocktail.findAll({
limit,
offset,
order: [[`createdAt`, 'DESC']],
where: {
[Op.or]: [
{
name: {
[Op.iLike]: `%${searchTerm || ''}%`,
},
},
{
description: {
[Op.iLike]: `%${searchTerm || ''}%`,
},
},
],
},
include: [
{
model: Ingredient,
as: 'ingredients',
attributes: {
exclude: ['createdAt', 'updatedAt', 'Cocktail_Ingredient'],
},
},
{
model: Step,
as: 'steps',
attributes: {
exclude: ['createdAt', 'updatedAt', 'Cocktail_Step'],
},
},
],
})
.then((cocktails) => {
return res.status(200).send({ count: data.count, limit, cocktails });
})
.catch(() => res.status(500).send({ message: 'error here too' }));
});
编辑:
使用以下方法将我的 iLike
查询转换为 MySQL 友好:
Sequelize.where(Sequelize.fn('lower', Sequelize.col('name')), {
[Op.like]: `%${searchTerm || ''}%`,
}),
只有LIKE,没有ILIKE
。
Case sensitivity 的内容有所不同。
代替 I LIKE,您可以使用 LOWER() 和 LIKE 来忽略区分大小写。
示例:SELECT * FROM products WHERE LOWER(productName) LIKE LOWER('%my search terms%');