0

This question is about your opinion and common approach for relational databases. So please, do not vote me down. I really need to solve this problem.. I remove this question once I meet the answer.

I have a response from my server sth like this:

"groups":[  
              {  
                 "id":"ea369ff6-cbc3-44b6-8eb6-b79e8db0145c",
                 "ingredients":[  
                    {  
                       "id":"f5126c1f-11a7-5ce5-a717-c6c86483058e",
                       "name":"First",
                       "position":1
                    },
                    {  
                       "id":"dfcf217e-53dd-b06c-9f2b-084f8180ba1a",
                       "name":"Second",
                       "position":2
                    }
                 ]
              },
              {  
                 "id":"0fa91ada-1fe4-4d6d-b772-caae44758848",
                 "position":2,
                 "ingredients":[  
                    {  
                       "id":"9cacade2-92fe-bd5c-c84c-7c5fe36c8a5e",
                       "name":"Third",
                       "position":1
                    },
                    {  
                       "id":"f5126c1f-11a7-5ce5-a717-c6c86483058e",
                       "name":"First",
                       "position":2
                    }
                 ]
              }
           ]
  • Here are two groups. Each of them has unique id.
  • Every group has some ingredients. They are the same type
  • Every group has ingredient with name First. This ingredient has the same id in every group, which means, it is the same object.

but

First ingredient has different values of position depending on where it is located.

The question is:

Should they have the same id while they are not the same? What is good approach for this problem? I need simple, quick and clarified answer.

Doesnt id mean that this is unique along with the same type of object? What If I need to save it to core data or other persistent store on device?

Very thanks.

3

Normalizing this will result in three tables with an many:many relation, the columns in parens () indicate the logical Primary Key:

groups: (group_id)

ingredients: (ingredient_id), name

ingredients_per_group: (group_id, ingredient_id), position

This is the junction table between groups and ingredients, the position is just an attribute of the ingredient in this specific group.

If an ingredient could be used many times in the same recipe but in different positions, (say use some butter in step 2 but also in step 17), then the PK of ingredients_per_group should be (group_id, ingredient_id, position).

It's like an ingredient for a receipt, the same ingredient can/will be used in multiple receipts, but in different amounts or in a different order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.