In this article, we will learn about AdonisJS Model relationships, how Adonis supports these relationship types, the different types of database relationships, and how to define these relationships.

Model Relationship Options

Defining AdonisJS Model relationships within our models unlocks a whole lot of power when it comes to querying and persisting data within our application database.

Here, within our model, we will use both types and decorators to define a relationship.

love-to-code-Cloudsurph

AdonisJS provides us with a decorator and a type for each type of relationship.
You can purchase your hosting from Cloudsurph.comCloudsurph hosting is a reliable hosting option for business and personal projects. We offer insight and help on system configuration issues and code errors or bugs.

So now, to start, we need to first understand the relationships and what we need to define for our users, then we will take a look at the options Lucid provides to us, and for more information need we can see Adonis’s main site.

Defining Our User’s Relationships

If we inspect our User model, you will see we don’t have a single relatable column defined.

However, if we review our other models you will see both Project and Task contain columns that related back to our User.

So, Let’s walk through these relationships and their relationship types:

Tasks=>created_by=> It is a many-to-one relationship from our User and our user can create many tasks, but a task can only be created by one user.

tasks=>assigned_to=> It is a many-to-one relationship with our User and the user can have many tasks assigned to them, but a task can only be assigned to one user.

project_users=>user_id=> It is a many-to-many and a user can be a member of many projects and a project will have many users.

Love to Code? We’re Your Helping Partner, click here for  Buy Our Service

How Do I Know Which to Use?

We can enter our relationships and have them correlate pretty closely to one of Adonis’s relationship names.

So, one of our relationships is here to see how to determine which relationship type to use for a given relationship, and here we can follow some basic ideas:

**If my user can have many tasks, then we can use HasMany to define this relationship from our User.

**If my task belongs to a specific user, then we can use BelongsTo to define the relationship between our Tasks.

**Finally, we just need to define both sides of this relationship and use HasMany on our User model and BelongsTo on our Task model.

How to Define A Relationship

Now if we’ve described our user’s relationships and what our relationship options are, then we can learn how to define our relationships.

You can check our previous article: AdonisJS: REST API simple CRUD Operation. IF you want then buy a good, reliable, secure web hosting service  from here: click here

So, for our user’s relationship to task’s created_by column and we can define the many-to-one relationship like below:

import { hasMany, HasMany, /* ... */ } from '@ioc:Adonis/Lucid/Orm'
import Task from 'App/Models/Task'

export default class User extends BaseModel {
/* other fields */

@hasMany(() => Task, {
foreignKey: 'createdBy'
})
public tasks: HasMany<typeof Task>
}

Now, we have one side of our relationship defined and we need to hop over to our Task model and define the other side of the relationship.

import { column, belongsTo, BelongsTo, /* ... */ } from '@ioc:Adonis/Lucid/Orm'
import User from 'App/Models/User'

export default class Task extends BaseModel {
/* other fields */

@column()
public createdBy: number

@belongsTo(() => User, {
localKey: 'createdBy'
})
public user: BelongsTo<typeof User>
}
So, for example, our relationship will look like the below:
export default class Task extends BaseModel {
@column()
public userId: number

@belongsTo(() => User)
public user: BelongsTo<typeof User>
}

Completing User Relations

So, here we have one of our User relationships done and dusted and our assignedTo relationship is going to be practically identical to our createdBy.

Assigned To (Many-To-One)

This is thanks to the relationship itself being defined by the decorator and we cannot have duplicates, since that would lead to overwriting a relationship.

So, for our assignedTo to definition, we can change the property name from our tasks to assignedTasks.

import { hasMany, HasMany, /* ... */ } from '@ioc:Adonis/Lucid/Orm'
import Task from 'App/Models/Task'

export default class User extends BaseModel {
/* other fields */

@hasMany(() => Task, {
foreignKey: 'createdBy'
})
public tasks: HasMany<typeof Task>

@hasMany(() => Task, {
foreignKey: 'assignedTo'
})
public assignedTasks: HasMany<typeof Task>
}
Next, we can define the other side of this relationship as below:
import { column, belongsTo, BelongsTo, /* ... */ } from '@ioc:Adonis/Lucid/Orm'
import User from 'App/Models/User'

export default class Task extends BaseModel {
/* other fields */

@column()
public createdBy: number

@column()
public assignedTo: number

@belongsTo(() => User, {
localKey: 'createdBy'
})
public user: BelongsTo<typeof User>

@belongsTo(() => User, {
localKey: 'assignedTo'
})
public user: BelongsTo<typeof User>
}
However, we can rename this property to something that makes sense for our relationship.
import { column, belongsTo, BelongsTo, /* ... */ } from '@ioc:Adonis/Lucid/Orm'
import User from 'App/Models/User'

export default class Task extends BaseModel {
/* other fields */

@column()
public createdBy: number

@column()
public assignedTo: number

@belongsTo(() => User, {
localKey: 'createdBy'
})
public creator: BelongsTo<typeof User>

@belongsTo(() => User, {
localKey: 'assignedTo'
})
public assignee: BelongsTo<typeof User>
}

Here it will be probably making the most sense to change our createdBy’s user property name to creator and our assignedTo’s user property name to the assignee.

import { hasMany, HasMany, manyToMany, ManyToMany, /* ... */ } from '@ioc:Adonis/Lucid/Orm'
import Task from 'App/Models/Task'
import Project from 'App/Models/Project'

export default class User extends BaseModel {
/* other fields */

@hasMany(() => Task, {
foreignKey: 'createdBy'
})
public tasks: HasMany<typeof Task>

@hasMany(() => Task, {
foreignKey: 'assignedTo'
})
public assignedTasks: HasMany<typeof Task>

@manyToMany(() => Project)
public projects: ManyToMany<typeof Project>
}

User Projects (Many-To-Many)

For the last relationship, we need to assign our User the many-to-many relationship for our user-to-project relationship.

export default class User extends BaseModel {
/* other fields */

@manyToMany(() => Project, {
pivotTable: 'project_users'
})
public projects: ManyToMany<typeof Project>
}

If you want to know more about AdonisJS Model relationships then please visit AdonisJs main website.

If you enjoyed reading this article and have more questions please reach out to our support team via live chat or email and we would be glad to help you. we provide server hosting for all types of need and we can even get your server up and running with the service of your choice.