node.js - Sequelize get random entry with association and limit, "缺少FROM子句条目

标签 node.js postgresql sequelize.js

我试图从表及其关联中获取随机条目。

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;

最佳答案

this 开始,似乎不支持在与 where 相同的级别上使用 include线程在 2015 年。目前尚不清楚您的解决方案是什么。

sequelize 贡献者在上述主题中的相关引用:

include.where won't since you need a negative query. You could probably try a raw where. .and({filter_level: ...}, ['RAW QUERY']})

附带说明.. 如果您没有完全致力于使用 sequelize,我个人建议使用 knex.js .我在生产代码中使用它。这是一个很好的平衡库。您可以获得像 sequelize 这样的 ORM 的很多灵 active /易用性,而不会失去对查询本身的太多视线/控制。如果库不支持您的操作,添加/运行原始查询非常容易。

这很粗糙,因为我很确定要么 Sequelize 使这个过于复杂,或者您的数据库结构过于复杂。这 不应该需要 4 个连接!这是总体思路:

  1. 假设您的数据库自动增加食谱 ID

  2. 使用 COUNT() 从食谱表中获取食谱数量

  3. 生成一个介于 0 和 COUNT() 之间的随机数,这是你的随机食谱的 id

  4. 查询食谱的食谱表,在产品表上左联接以获取,在食谱 ID 上联接。这是一个可以用 knex 轻松编写的简单查询。

  5. 利润!您现在有一个随机食谱和该食谱所需的所有产品。

关于node.js - Sequelize get random entry with association and limit, "缺少FROM子句条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38726228/

相关文章:

hibernate - setParameter() 没有设置正确的引号

postgresql - 使用日期过滤器从点创建缓冲区 postgis 表

express - 多次更新后发送单个响应

postgresql - 在 sequelize ENUM 类型上设置默认值

node.js - 在不重置数据库的情况下在 sequelize 中添加列

node.js - Node js - 为什么需要 3 个连接?

Node.js + Express + Handlebars.js + 局部 View

postgresql - 带有遗留数据库的 grails 2.2.5 中的复合键和映射

javascript - Gulp 任务合并 Javascript 和框架

node.js - Socket.io pinginterval 对服务器性能的影响