What does it mean for a One-to-many Link relation

2019-08-01 21:28发布

问题:

i drug a line between two tables in the Linq Object Relational (O/R) mapper:

 Order                  Customer
--------------           ---------------
| OrderID    |           | CustomerID  |
| CustomerID |♦---------˃| ...         |
|            |           |             |
--------------           ---------------

Note: Or perhaps it was

 Order                    Customer
--------------           ---------------
| OrderID    |           | CustomerID  |
| CustomerID |˂---------♦| ...         |
| ...        |           |             |
--------------           ---------------

i'm not sure; it lets me drag both ways.

  • First question, what's the arrowhead, and what's the diamond?

Assuming the second diagram, the cardinality of the Association was created as OneToMany. This makes sense, since:

  • one customer
  • has many orders

But what confuses me is the Association.Unique (Boolean) property. It defaults to false. This makes sense because it's a OneToMany association. Order.CustomerID cannot be unique, otherwise it wouldn't be a OneToMany association, it would be OneToOne.

But then i'm allowed to change the OneToMany Unique property to true. This makes no sense, so i conclude that Unique ness doesn't apply to Order.CustomerID, but instead to Customer.CustomerID. But the diagram already indicates Customer.CustomerID is a Primary Key. Of course it's unique, it's a primary key.

But the Unique property isn't set. This makes no sense, so i conclude that Unique ness doesn't mean either table.

  • Second question, what does Unique mean?

    Specifies whether the foreign target columns have a uniqueness constraint

  • Third question: What is parent and child?

Assuming, again, the second diagram:

Customers.CustomerID ♦------------> Orders.CustomerID

i take Customers table to be a parent. It's the one who owns what it means to be a customer. You want to change something about a customer, you walk to the parent.

Meanwhile, the child Orders table comes along and wants to reference a Customer.

     Parent(diamond)                     Child(arrowhead)
====================                     =================
Customers.CustomerID (PK) ♦------------> Orders.CustomerID (FK)

Except that when i look at the association's Parent and Child properties:

Child property

  • Name: Orders

Parent property

  • Name: Customer

They want to create a property on the "child" called Orders. No, no, no. The child is orders. And they want to add a property to the parent called Customer. No, no no. The parent is a customer.

That means i must have it backwards, and the terms parent and child are the exact opposite of what i thought:

      Child(diamond)                     Parent(arrowhead)
====================                     =================
Customers.CustomerID (PK) ♦------------> Orders.CustomerID (FK)

And by this time i want to blow my brains out; and instead spend 35 minutes authoring a question on Stackoverflow; rather than continuing to scream at my computer.

Help.

回答1:

Regarding question 2 - The Unique property probably signifies that the relation is a OneToOne relation.

Even directly in a sql database there is no specific OneToOne relationship. Rather there is simply the foreign key where a key in one table can be exported to another table. As such a foreign key relationship is ALWAYS one to many. Using additional constraints on the table containing the exported key, the relationship can be made de facto one to one. But doing that doesn't change the foreign key relation, rather it simply applies additional conditions on the data in the foreign key table.

Regarding question 3: The property names mean that the child object (Order) will have a property called Customer which can be used to get an instance of the parent object. And the parent object will have a property called Orders which can be used to fetch children for the customer.