views:

183

answers:

3

I have a many-to-many relationship between two tables: Order and Item. I need to save an extra information on this relationship, the quantity.

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

In the following schema, both orderItems are to-many relationship to OrderItem table. order & item are inverse relationship.

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

Edited:

So according to Randy, is this what you suggest?

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

orderItems points to Item as a to-many relationship, the inverse relationshipo is orders, orders points to Order as a to-many relationship

+3  A: 

There is no need to create an additional table. It's acceptable to have a M2M association table that contains columns other than the FK references to the two tables. Sometimes an additional column in the M2M association table makes perfect sense.

Randy Minder
hi, I edited the question, can you conferm on the schema! thanks
sfa
@Hoang - Yes, that looks fine to me. The OrderItem table would have a reference to the Order table, a reference to the Item table and a Quantity column to hold the quantity for the association.
Randy Minder
Hi Randy, why you mentioned again OrderItem table in the comment? I have edited the question to delete the OrderItem table, and you said yes, that looks fine. Maybe I have not totally understood your idea?
sfa
@Hoang - You need the OrderItem table to properly represent your M2M association. It should have a reference to the Order table, Item table and the new Quantity column.
Randy Minder
thank Randy!!!!
sfa
+2  A: 

I am pretty sure the question you are asking is:

So, I have to create an extra entity in my model .xcdatamodel?

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

Just as you have described:

  1. An order item has exactly one order and one item.
  2. The orders have many order items and items are used by many order items.

Order <-->> OrderItem <<--> Item

The quantity attribute goes in the OrderItem entity.

This does not mean you are creating an extra table. If you are using SQLite for storage, Core Data would use an additional table for the many-to-many relationship anyway.

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

gerry3
yes, you cleared my doubt now! thank gerry
sfa
A: 

Hello, I'm with the same problem. When we create a many-to-many relationship in Core Data, it creates a relationship table, don't it? Is there a way to put the information (relationship attribute) in that table???

I've already thought about the two ways, but none of them appears to be the best. Please, correct me if I was wrong... I'm trying to modeling my app too.

1) It resolved the problem, but don't use the many-to-many relashionship from Core Data.

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

I prefer this because it don't duplicates lines.

2) This way we replicates the data, don't we? Because if you have the same item in more than one order, you have more than one row with the name information. And the problem increase if the item has more columns like description... The description will be replicated.

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

3) I'm reading now the Manual "Core Data" and thought in another possibility that it is equals the solution 2 in the database:

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

OrderedItem's parent = Item

                Item
                 ^
                 |
 Order <--->> OrderedItem

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

wal
hi, gerry3 has the answer, you need to create the new entity.
sfa
I know I need to create another entity, but I was thinking was the most elegant and economical way...
wal