Query builder .with() operator returning empty array

I know this is not a framework bug, but not sure what’s wrong here. I have an entity (Dish) and an entity (Steps). A step holds a FK reference to the dishes table. A Dish hasMany Steps and a Step belongsTo a Dish (both relationsihips defined on the Lucid Model). When I query a Dish by id with steps, it returns an empty array (when I see in the db that there is indeed a step associated with this dish:

const dish = await Dish
                .query()
                .with('steps')
                .where('id', id)
                .fetch()
const results = dish.toJSON();
return response
    .status(200)
    .json(results)

Happy to post any additional needed code snippets as well

You want to get the dish by STEP id?

I want to get the dish by dishId, populating steps. I previously tried with the .where() before the .with() call, but same results of empty array

Can you share your both models and migrations code?

Absolutely!

class DishSchema extends Schema {
  up () {
    this.create('dishes', (table) => {
      table.increments()

      table
        .string('title')
        .notNullable()

      table.string('description')

      table
        .integer('user_id')
        .unsigned()
        .references('id')
        .inTable('users')

      table.timestamps()

    })
  }

  down () {
    this.drop('dishes')
  }
}
class Dish extends Model {
  steps() {
    return this.hasMany('App/Models/Step')
  }

  ratings() {
    return this.hasMany('App/Models/Rating')
  }
}
class StepSchema extends Schema {
  up () {
    this.create('steps', (table) => {
      table.increments()
      table
        .integer('step_number')
        .notNullable()
      table
        .string('instruction')
        .notNullable()
      table
        .integer('dish_id')
        .unsigned()
        .notNullable()
        .references('id')
        .inTable('dishes')
        .onDelete('CASCADE') // if a dish is deleted, delete the steps associated with it as well
      table.timestamps()
    })
  }

  down () {
    this.drop('steps')
  }
}
class Step extends Model {
    dish() {
        return this.belongsTo('App/Models/Dish')
    }
}

Try using this code in your dish model:

class Dish extends Model {
  steps() {
    return this.hasMany('App/Models/Step', 'id', 'dish_id')
  }

  ratings() {
    return this.hasMany('App/Models/Rating')
  }
}

Tell me if you get something with this

Unfortunately I see the same results here. Is that how Lucid/Adonis works internally as a default?

Can you show your dishes and steps rows in database?

Can you please turn on database debugging and see the queries being executed by the framework?

Yes, absolutely.


Apparently, I can only upload one image per comment, so next one to come in the next comment. Thanks for your help!

This is the Step row

Yes, absolutely. I turned on debugging and am now logging for that query. Would you like to look into anything in particular?

In the last object logged, the response seems to be correct. However, the JSON response is still an empty array

Can you share the query log here?

I wasn’t sure the best way to export these, so hopefully the screenshots suffice. I will attach the two relevant logs, but will note that there was an initial log for the user as well. I didn’t write a user query on this route but maybe this is Adonisjs internal middleware? I can share that log as well, but it didn’t seem as relevant to me.

Let me know if there is a better way to share these. Thanks for checking it out!