Many-to-many relationship with optional value?
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, i开发者_开发百科tem)
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
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.
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:
- An order item has exactly one order and one item.
- 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.
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
精彩评论