Many-to-many relationship with added value?

I have a many-to-many relationship between two tables: Order and Item. I need to save additional information about this connection, quantity.

So, do I need to create an extra table in my .xcdatamodel model?

In the following diagram, both orderItems refer to many of the OrderItem table. order and item are inverse relationships.

 Order (start, end, orderItems) Item (name, orderItems) OrderItem (quantity, order, item) 

Edited by:

So, according to Randy, is that what you offer?

 Order (start, end, orderItems) Item (name, quantity, orders) 

orderItems points to item as a to-many relationship, inverse - orders , orders points to order as a to-many relationship

+4
source share
3 answers

There is no need to create an additional table. It is acceptable to have an M2M association table containing columns other than FK references for the two tables. Sometimes an extra column in the M2M association table makes sense.

+3
source

I am sure the question you asked is:

So, do I need to create an additional object in my .xcdatamodel model?

And the answer is YES. You will need a third "OrderItem" object.

As you described:

  • An order item has exactly one order and one item.
  • There are many order elements in orders, and elements are used by many order elements.

Order ↔> OrderItem ↔ Item

The quantity attribute is passed to the OrderItem object.

This does not mean that you are creating an additional table. If you use SQLite for storage, Core Data will use an extra table for many-to-many relationships.

Typically, with Core Data, you will design and use a data model that fits your needs. You should not think about it in terms of SQL or tables. In fact, you don’t even need to use SQL for storage.

+3
source

I have the same problem. When we create many-to-many relationships in Core Data, it creates a relationship table, doesn't it? Is there any way to put information (relation attribute) in this table?

I already thought about these two methods, but none of them seems to be the best. Please correct me if I am wrong ... I am also trying to model my application.

1) He solved the problem, but did not use the many-to-many binding from Core Data.

 Order (start, end, orderItems) Item (name, orderItems) OrderItem (quantity, order, item) 

I prefer this because it does not duplicate rows.

2) So we are replicating the data, right? Because if you have the same item in multiple orders, you have multiple lines with name information. And the problem increases if the element has more columns, such as a description ... The description will be replicated.

 Order (start, end, orderItems) Item (name, quantity, orders) 

3) Now I read the guide “Basic data” and thought that it is equal to solution 2 in the database:

 Order (start, end, orderedItens) Item (name, description, ...) OrderedItem (quantity, order) 

OrderedItem parent = Item

  Item ^ | Order <--->> OrderedItem 

Model at http://www.freeimagehosting.net/uploads/f6ca00bc2f.png

0
source

Source: https://habr.com/ru/post/1301118/


All Articles