我试图从表及其关联中获取随机条目。
Recipe.findAll({
order: [
[sequelize.fn('RANDOM')]
],
where: {
'$RecipeCategory.name$': category
},
include:[
{
model: models.Category,
as: 'RecipeCategory',
},{
model: models.Product,
}],
subQuery:false,
limit:1})
使用上面的代码,我得到一个Recipe的随机条目及其与限制1的关联。例如,它只返回1个产品,我需要所有产品。我需要为所有产品准备一份食谱。
有什么建议吗?
如果我删除了subQuery选项,我会收到: SequelizeDatabaseError:表“RecipeCategory”缺少FROM子句条目
我在5天内搜索解决方案,我认为我已经检查了所有相关的答案。'
编辑:生成的查询
SELECT "Recipe"."id", "Recipe"."name", "Recipe"."description", "Recipe"."pic", "Recipe"."createdAt", "Recipe"."updatedAt", "RecipeCategory"."id" AS "RecipeCategory.id", "RecipeCategory"."name" AS "RecipeCategory.name", "RecipeCategory"."description" AS "RecipeCategory.description", "RecipeCategory"."createdAt" AS "RecipeCategory.createdAt", "RecipeCategory"."updatedAt" AS "RecipeCategory.updatedAt", "RecipeCategory.Recipes_Categories"."createdAt" AS "RecipeCategory.Recipes_Categories.createdAt", "RecipeCategory.Recipes_Categories"."updatedAt" AS "RecipeCategory.Recipes_Categories.updatedAt", "RecipeCategory.Recipes_Categories"."CategoryId" AS "RecipeCategory.Recipes_Categories.CategoryId", "RecipeCategory.Recipes_Categories"."RecipeId" AS "RecipeCategory.Recipes_Categories.RecipeId", "Products"."id" AS "Products.id", "Products"."name" AS "Products.name", "Products"."protein" AS "Products.protein", "Products"."fat" AS "Products.fat", "Products"."carbohydrates" AS "Products.carbohydrates", "Products"."salt" AS "Products.salt", "Products"."min" AS "Products.min", "Products"."max" AS "Products.max", "Products"."vegan" AS "Products.vegan", "Products"."piece" AS "Products.piece", "Products"."createdAt" AS "Products.createdAt", "Products"."updatedAt" AS "Products.updatedAt", "Products.Product_Recipe"."position" AS "Products.Product_Recipe.position", "Products.Product_Recipe"."createdAt" AS "Products.Product_Recipe.createdAt", "Products.Product_Recipe"."updatedAt" AS "Products.Product_Recipe.updatedAt", "Products.Product_Recipe"."ProductId" AS "Products.Product_Recipe.ProductId", "Products.Product_Recipe"."RecipeId" AS "Products.Product_Recipe.RecipeId"
FROM
"Recipes" AS "Recipe"
LEFT OUTER JOIN (
"Recipes_Categories" AS "RecipeCategory.Recipes_Categories"
INNER JOIN
"Categories" AS "RecipeCategory" ON "RecipeCategory"."id" = "RecipeCategory.Recipes_Categories"."CategoryId"
) ON "Recipe"."id" = "RecipeCategory.Recipes_Categories"."RecipeId"
LEFT OUTER JOIN (
"Product_Recipes" AS "Products.Product_Recipe"
INNER JOIN "Products" AS "Products" ON "Products"."id" = "Products.Product_Recipe"."ProductId"
) ON "Recipe"."id" = "Products.Product_Recipe"."RecipeId"
WHERE "RecipeCategory"."name" = 'meal-3'
ORDER BY RANDOM()
LIMIT 1;
答案 0 :(得分:0)
从2015年this线程开始,我们不支持在与include
相同的级别上使用where
。目前还不清楚解决方案的解决方案是什么你的情况。
一个续集的贡献者从上面的线索中的相关引用:
include.where赢了,因为你需要一个否定的查询。 你可能会尝试原始的地方。 .and({filter_level:...},[' RAW QUERY']})
侧面说明..如果你没有完全承诺使用续集,我个人建议使用knex.js。我在生产代码中使用它。它是一个很好的平衡库。你可以获得像sequelize这样的ORM的大量灵活性和易用性,而不会失去对查询本身过多的视觉/控制。如果图书馆不支持你的工作,那么添加/运行原始查询非常容易。
这非常粗糙,因为我很确定要么是续集 太复杂了,或者你的数据库结构过于复杂。这个 不应该需要4个连接!这是一般的想法:
假设您的数据库自动递增食谱ID
使用COUNT()
生成0到COUNT()之间的随机数,这是随机食谱的ID
查询配方表中的配方,左边的连接在产品表上获取,加入配方ID。这是一个简单的查询,可以在knex中轻松编写。
利润!你现在有一个随机食谱和食谱所需的所有产品。