AdonisJS: is something wrong with belongsTo() relationship design? In AdonisJs framework, suppose we have a Profile model in which we define belongsTo() relationship to theUser model. The documentation says this will lead us to define our databases this way:



The column id in users table is a foreign key referencing profiles tables instead of being the primary key of users table. Can this 'bad DB design' lead to some inconsistencies? 

Billal Begueradj
I think that the only problem with the linked documentation are the typos in the model image:

The users column id (FK) was meant to say id (PK).
The profiles column user_id (PK) was meant to say user_id (FK).
The link should have reverse direction (in all other diagrams, links start from the PK/referenced table and point to the Fk/referencing table).

The diagram under the belongsTo() example seems to be the correct one (and should be used in hasOne(), too).

The design shows a 1-1 relationship, or more accurately a 1 - 0..1 relationship, between users and profiles.

A user can have one (hasOne) profile. A profile belongs to (belongsTo) one user.



What seems to be missing from the diagrams is that profiles (user_id) should have a UNIQUE constraint, in order for the relationship to be true 1-1 and not 1-to-many. Perhaps the ORM creates such a constraint and it was missed when drawing the diagrams.