Node.js Sequelize get random entry with association and limit;从句条目中缺少

Node.js Sequelize get random entry with association and limit;从句条目中缺少,node.js,postgresql,sequelize.js,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.findAll({
order: [
  [sequelize.fn('RANDOM')] 
],
where: {
  '$RecipeCategory.name$': category
},
include:[
{
  model: models.Category,
  as: 'RecipeCategory',
},{
  model: models.Product,
}],
subQuery:false,
limit:1})
通过上面的代码,我得到了一个配方的随机条目及其与限制1的关联。例如,它只返回1个产品,而我需要所有产品。我需要一份所有产品的配方

有什么建议吗

如果删除子查询选项,我将收到以下信息: SequelizeDatabaseError:表“RecipeCategory”的子句条目中缺少

我在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;

它看起来像是在与
相同的级别上使用
include
,其中自2015年起不支持
。目前还不清楚你的情况下会有什么样的解决方案

sequelize撰稿人对上述主题的相关引用:

include.where不会,因为您需要否定查询。 你也许可以试试生的。和({filter_level:…},['RAW QUERY']})

顺便说一句。。如果您没有完全承诺使用sequelize,我个人建议您使用。我在生产代码中使用它。这是一个很好的平衡图书馆。像sequelize这样的ORM具有很大的灵活性/易用性,而不会对查询本身失去太多的视觉/控制。如果库不支持您的操作,那么添加/运行原始查询非常容易

这是相当粗糙的,因为我很确定 是过于复杂还是数据库结构过于复杂。这 不需要4个连接!以下是总体思路:

  • 假设您的数据库自动增加配方id

  • 使用COUNT()从recipes表中获取配方数

  • 生成一个介于0和COUNT()之间的随机数,这是随机配方的id

  • 查询配方表中的配方,在products表中留下join以获取,join on recipe id。这是一个简单的查询,可以在knex中轻松编写

  • 利润!您现在有了一个随机配方和配方所需的所有产品


  • 它看起来像是在与
    相同的级别上使用
    include
    ,其中自2015年起不支持
    。目前还不清楚你的情况下会有什么样的解决方案

    sequelize撰稿人对上述主题的相关引用:

    include.where不会,因为您需要否定查询。 你也许可以试试生的。和({filter_level:…},['RAW QUERY']})

    顺便说一句。。如果您没有完全承诺使用sequelize,我个人建议您使用。我在生产代码中使用它。这是一个很好的平衡图书馆。像sequelize这样的ORM具有很大的灵活性/易用性,而不会对查询本身失去太多的视觉/控制。如果库不支持您的操作,那么添加/运行原始查询非常容易

    这是相当粗糙的,因为我很确定 是过于复杂还是数据库结构过于复杂。这 不需要4个连接!以下是总体思路:

  • 假设您的数据库自动增加配方id

  • 使用COUNT()从recipes表中获取配方数

  • 生成一个介于0和COUNT()之间的随机数,这是随机配方的id

  • 查询配方表中的配方,在products表中留下join以获取,join on recipe id。这是一个简单的查询,可以在knex中轻松编写

  • 利润!您现在有了一个随机配方和配方所需的所有产品


  • Console记录查询并在问题中包含Console.log。有关记录查询的说明,请参阅此链接Console记录查询并在问题中包含Console.log。有关记录查询的说明,请参阅此链接