简体   繁体   中英

Setting up postgres database for recipes

I am new to PostgreSQL and trying to plan a database that will allow me to query recipes based on id, ingredients etc from a provided dataset, example below.

I am getting a bit thrownoff by the nested ingredients and how to lay out my tables.

I was initially thinking two tables one for instructions and image and another for the ingredients.

i was now wondering if i am on the right lines and if so how to loop through the nested ingredients to produce a useful second table

enter code here

  [  {
 "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 }
 ]
},]

I have parsed the json you provided and loaded it in a table. Check the dbfiddle link .

Here is the parsed content loaded into a table.

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)

Note : I had changed the JSON key value from "imageUrl" to "image" because possibly "url" which is a keyword for postgres might have been impeding the parsing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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