Lucid Models: How to access linked models?

Hi,

this is probably a noob question, but couldn’t figure it out using the documentation.

How to I access linked model objects from another object?

I have these Models:

'use strict'

const Model = use('Model')

class Door extends Model {

	logs() {
		return this.hasMany('app/Models/Logs')
	}

}

module.exports = Door
'use strict'

const Model = use('Model')

class DoorLog extends Model {

	door() {
		return this.hasOne('app/Models/Door')
	}

}

module.exports = DoorLog

And then I would like to do something like this (get first dor, and list that doors related door logs):

Route.get('test', async () => {
	return await Door.find(1).logs()
})

but find(1) is not return the Door object. How do I actually use these relationships?

Try relationships like this

const report = await Report.query().select('*')
.with('report_categories')
	.scope('report_categories', (builder) => {
		
	builder.select('report_categories.*','categories.image as category_image');
		
		builder.where('deleted', '0');
		builder.join('categories','categories.id','report_categories.category_id');

	})		
.where('id','=',data.report_id)
.first();

And let me know the results

Isn’t there a more elegant way of doing this? This seems to me just like writing regular SQL statements. I was hoping they would behave more like objects with relations.

Sorry, I am newbie for adonis 4.0 I only know this way. Will let you know when I found a new way. For more details please refer to http://knexjs.org/ regarding structure

The old documentation has more details about relationships: https://adonisjs.com/docs/3.2/relationships

By reading it, I should be able to access door with id 1’s logs with this:

Route.get('test', async () => {
	const door = Door.find(1)
	const logs = door.logs().fetch()
	return logs
})

But it gives me this error:

TypeError
door.logs is not a function

Strange I am also getting the same error using new convention.

User.query(…).where(…).where(…).select(…).app_install_requests is not a function

I got it working as I wanted now :slight_smile:

The problem was that I was either using yield or not using yield/await at all.

When I used the proper await 4.0 syntax on each statement it works perfectly:

Route.get('test', async () => {
	const center = await Center.find(1)
	const door = await center.doors().first()
	const log = await door.logs().fetch()
	return log
})
1 Like

I am trying has one relationship: One-to-one relationship between employees and salaries.
one employee has one relationship with salaries.

My Database :

employee

  class EmpSchema extends Schema {
    up () {
      this.create('employees', table => {
      table.integer('emp_no').primary().notNullable().unique();
      table.string('firstname', 20).notNullable();
      table.string('lastname', 20);
      table.enu ('gender', ['m','f']).notNullable();
      table.date('hiredate').notNullable();
      table.timestamps();
     })
   }

  down () {
    this.drop('employees')
  }
   }

Salaries

         class SalariesSchema extends Schema {
       up () {
           this.create('salaries', (table) => {
            table.integer('emp_no').index().references('emp_no').inTable('employees').onDelete('CASCADE');
            table.integer('salary').notNullable();
            table.date('from_date').notNullable();
            table.date('to_date').notNullable();
            table.primary(['from_date']);
         })
          }

         down () {
           this.drop('salaries')
       }
       }

Models

employee

 salaries(){
        return this.hasOne('App/Models/Salaries','emp_no','emp_no');
     }

salaries

    salaries(){
     return this.hasOne('App/Models/Salaries','emp_no','emp_no');
     }

route

    Route.get('find', 'EmpsalaryController.Showid');

Showid

   var getUser = await Employee.query().where('emp_no', '=', emp_no);
         console.log(getUser);
      var empSalary = await getUser.salaries().fetch();
      return response.status(200).json({
        data: empSalary,
        message: 'get the record',
        status: true
      });

          i got this error: getUser.salaries is not a function

Replied you on github https://github.com/adonisjs/adonis-framework/issues/753