Is my relation a one-to-many or a many-to-many?

468 Views Asked by At

I have a model called Addresss which as the name sounds, is a list of addresses.

These addresses can belong to a Client and a client can have many of these addresses.

To link these addresses to the client, I will simply have a table called ClientAddress with 3 columns: id, client_id and address_id.

Is this an example of a one to many or a many-to-many relationship? I currently have it setup as a ManyToMany relationship in Phalcon however I'm not sure if it should actually be One to Many.

3

There are 3 best solutions below

0
On

It's a one-to-many relation. One client (can) have multiple addresses. One address belongs to only one client.

Regarding your clientAddress table, I'd get rid off it as you can store the client id on the adress table.

If, as your tags suggest you're using phalcon and decide do go with phalcon's orm you should have a look at the documentation : Working with Models

0
On

Its all depends how you are thinking about your Entities

Lets start from Client
We would like to store information about Client. Now if our Client has only one specific Location then we have two options. We can directly store the Address information in same table as-

clients table id, f_name, l_name, address, current_city, home_city, etc.... In this case there is nothing about Relation

If you are interested then you can split this table and store Location information on other table which you may name as addresses. Then the Relation between Client and Address will be one-to-one relation.

Now, if our Client has different office on different Location then it is mandatory to store Location information on different table. Then the Relation will be one-to-many as our Client has different `Location'.

Now, if we have many Client on same Location (same building may be) and our Client has many office in many Location then it will be as many-to-many relation. As Client hasMany Address and Address hasMany Client we new a pivot or intermediate table to hold our Relation information.

0
On

It depends on quite how complex your model is likely to become.

Suppose your "clients" are branches of a national company. Eech "client" may then have multiple adrresses - delivery address and billing address for instance. And equally, since the accounts-payable function may be centralised to either a regional or national branch (which may itself have, or not have a "delivery address)" the billing address is likely to be shared amongst many "clients."

So, in this scenario, many-to-many.