繁体   English   中英

为食谱设置 postgres 数据库

[英]Setting up postgres database for recipes

我是 PostgreSQL 的新手,正在尝试规划一个数据库,该数据库允许我从提供的数据集中根据 ID、成分等查询食谱,如下例所示。

我对嵌套的配料以及如何布置我的桌子感到有点厌恶。

我最初想的是两张桌子,一张是说明和图片,另一张是配料。

我现在想知道我是否在正确的路线上,如果是这样,如何循环遍历嵌套的成分以生成有用的第二张表

在这里输入代码

  [  {
 "id": "recipe-88",
 "imageUrl": "http://www.images.com/12",
 "instructions": "blend with oat milk and ice, sprinkle with 
 salt",
 "ingredients": [
   { "name": "blueberries", "grams": 114 },
   { "name": "coffee", "grams": 20 },
   { "name": "kale", "grams": 48 }
 ]
},
{
 "id": "recipe-74",
 "imageUrl": "http://www.images.com/2",
 "instructions": "crush ingredients with mortar and pestle, mix 
 with whole milk, serve in bowl",
 "ingredients": [
   { "name": "coffee", "grams": 25 },
   { "name": "lime", "grams": 140 },
   { "name": "strawberries", "grams": 3 },
   { "name": "apricots", "grams": 24 },
   { "name": "kale", "grams": 50 }
 ]
},
{
 "id": "recipe-77",
 "imageUrl": "http://www.images.com/25",
 "instructions": "blend with oat milk and ice, sprinkle with 
  salt",
 "ingredients": [
   { "name": "coconut", "grams": 14 },
   { "name": "coconut", "grams": 57 },
   { "name": "lime", "grams": 153 },
   { "name": "oat milk", "grams": 31 }
 ]
},]

我已经解析了您提供的 json 并将其加载到表中。 检查dbfiddle 链接

这是加载到表中的解析内容。

postgres=# select * from parsedjj;
 id | recipe_id |        imageurl         |                                 instructions                                  |                                                                                ingredients
----+-----------+-------------------------+-------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1 | recipe-88 | http//www.images.com/12 | blend with oat milk and ice, sprinkle with salt                               | [{"name": "blueberries", "grams": 114}, {"name": "coffee", "grams": 20}, {"name": "kale", "grams": 48}]
  2 | recipe-74 | http//www.images.com/2  | crush ingredients with mortar and pestle, mix  with whole milk, serve in bowl | [{"name": "coffee", "grams": 25}, {"name": "lime", "grams": 140}, {"name": "strawberries", "grams": 3}, {"name": "apricots", "grams": 24}, {"name": "kale", "grams": 50}]
  3 | recipe-77 | http//www.images.com/25 | blend with oat milk and ice, sprinkle with   salt                             | [{"name": "coconut", "grams": 14}, {"name": "coconut", "grams": 57}, {"name": "lime", "grams": 153}, {"name": "oat milk", "grams": 31}]
(3 rows)

注意:我已将 JSON 键值从“imageUrl”更改为“image”,因为 postgres 的关键字“url”可能阻碍了解析。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM