1

I am developing an instant message web application, and I decided to use MySQL to store my model. But I get a little confused when confronted with ORM persistence.

Say I have two models, User and Message. Message has two attributes related to User, sender and receiver. A user can send multiple messages while a message can only have one sender and one receiver.

My questions:

  1. According to this, one to many means

one row in table A may be linked with many rows in table B, but one row in table B is linked to only one row in table A

In my case, is User to Message a relationship of one to many? I mean, a message row is linked to two rows in the user table.

  1. If this is not a one to many relation, what actual relation is most suitable between User and Message?
philipxy
  • 14,867
  • 6
  • 39
  • 83
吴环宇
  • 337
  • 1
  • 6
  • 13
  • A "link" is from a column list in a table to a column list in a table. Not between tables. – philipxy Jun 02 '17 at 03:17
  • @philipxy So this is exactly a one to many relation.A sender column corresponds with only on row and so does receiver column.Am I right? – 吴环宇 Jun 02 '17 at 03:23
  • I can't understand your comment. See my answer. You need to talk either in terms of relationships represented by tables or in terms of FKs. PS A relation is not a relationship. A table (relation) represents a "relationship" (meaning association). A link is "relationship" (meaning FK). – philipxy Jun 02 '17 at 04:52
  • @philipxy sorry for my poor English. What you have explained in your answer is a bit abstruse but of great help.Thx! – 吴环宇 Jun 02 '17 at 08:46
  • Please comment on my answer if you think it could help make it more helpful. Re "abstruse" I hope you will consider how on the contrary it is concrete and practical. (Every paragraph has a precise example.) Yes "relationship" as association is *abstract* but it is the fundamental and practical notion that ER & the relational model are based on. Notice *your own link* uses "relationship" with three different meanings--association ("the relationship itself"), cardinality & FK ("link") while purporting to only give it *two* meanings (ER vs databases) & *muddling* all three. PS Edited my answer. – philipxy Jun 02 '17 at 10:51
  • Please give the *context* you are using "one to many" in (product/library/api) and reference(s) you are using. Eg in a particular ORM "1 to many" will be used in a particular way. – philipxy Jun 02 '17 at 19:16

3 Answers3

2

In ER Modeling, relationships/associations are on entities. In the relational model, relationships/associations are on values. (Many so-called ER diagrams don't actually show entities & relationships, but instead show their tables.)

Binary relationships have cardinalities. A table represents a relationship. Eg the relationship Sent(sender, message) "user sender sent message message". Which is 1 to many because each sender in it is paired with 1 or more messages in it. Eg the relationship Received(sender, message) "user receiver received message message". Which is 1 to many because each receiver in it is paired with 1 or more messages in it. Sometimes one relationship can be expressed in terms of others. Eg you have relationship Message(sender, message, receiver) "user sender sent message message to user receiver"; it is Sent(sender, message) AND Received(sender, message). It's non-binary; it doesn't have a binary cardinality; it has those other two binary relationships/cardinalities associated with it.

Eg: For "User to Message" where "a message row is linked to two rows in the user table" you seem to mean the relationship Involved(message, user) "message involved user user". In terms of the first two relationships, this is Sent(user, message) OR Received(user, message). In terms of the ternary relationship it's "FORSOME receiver, sender (Message3(user, message, receiver) OR Message3(sender, message, user))". It is 1 to 2 because each message in it is paired with 2 users in it.

Tables (base variables or query results) hold the rows that satisfy their relationships. A base table's rows are returned by a query that is its name. Relationship builders like AND, OR, FORSOME, renaming etc get their rows calculated by relation operators like JOIN, UNION, PROJECT, RENAME etc. Eg Involved is calculated per its first predicate expression above by the relation expression (RENAME sender\user Sent) UNION (RENAME receiver\user Received). Is there any rule of thumb to construct SQL query from a human-readable description?

FK (foreign keys) constraints get called "relationships". (But they are not.) They also get called "links". Each is associated with a statement expressible via its source and target tables' relationships. A FK is from a table column list to a table column list that forms a CK (candidate key). It says values in the first appear in the second. This happens when if a value satisfies the source table's relationship then it satisfies the target table's relationship. Eg for you, if sender sent message to receiver then sender is a user, and receiver is a user; so there are two corresponding FKs to User, one from {sender} to user and one from {receiver} to user. A FK from a first to a second table is many to one. (The cardinality of a certain relationship per its tables & lists.)

There are extended ways to talk about cardinalities. (Eg "0-or-1" to whatever. Eg look-here vs look-across.) When there are non-binary relationships there are generalized ways to express cardinalities. (Eg 1-to-M-to-N.) Methods account for SQL NULL cardinality 0 in different ways. Just as with tables, these are based on the binary relationship case above. Terminology depends on the modeling/diagramming method/product/style you are using. Find out about yours.

Find some academic textbooks/presentations/courses. The Wikipedia entry on Entity-Relationship Modeling currently isn't too bad. It addresses some confusions/misconceptions embodied by variant methods. Unfortunately most Wikipedia relational model references are poor.

PS I haven't used "model" because sometimes it means class, type, record, table, row, entity or relationship. You have to figure out what someone is saying. You have to make clear what you are saying if you use it.

philipxy
  • 14,867
  • 6
  • 39
  • 83
1

I think your domain is something like:

A user can send many messages.

A user can receive many messages.

A message has exactly one sender.

A message has exactly one recipient.

So, there are two one-to-many relationships from "message" to "user" - one for the sender, and one for the receiver.

Community
  • 1
  • 1
Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52
0

A RELATIONSHIP is 1-to-1, 1-to-many, or many-to-many, not a TABLE.

You have two relationships between the Message and User table: sender and receiver. EACH relationship is 1-to-many. The fact that there are two relationships doesn't change this.

Jay
  • 26,876
  • 10
  • 61
  • 112