AdonisJs Mysql Relationship is not working properly! Giving empty array whenever I try to fetch from database

#1

Currently I’m learning AdonisJs and Mysql. I was practicing demo pilot project like facebook status / comment system. Meanwhile I was trying to understand the relationship of mysql database. But Whenever I want to fetch all the comments associated with one particular status, the status controller produces empty comments array. I don’t know where exactly is the problem.

Here is my Status Model

const Model = use('Model')

class Status extends Model {

    comments() {
        return this.hasMany('App/Models/Comment', 'id', 'status_id')
    }


}

Here is my Comment model

class Comment extends Model {

    users() {
        return this.belongsTo('App/Models/User')
    }
}

Here is my Status Controller

const Status = use('App/Models/Status')
const Comment = use('App/Models/Comment')

class StatusController {

    async showAll({ request, response }){
        return await Status.query()
                            .with('comments')
                            .fetch()
        //return Comment.query().fetch()
    }

    async addStatus({ request, response, auth }) {
        let data = request.all()
        const user_id = await auth.user.id
        data.user_id = user_id
        const status = await Status.create(data)
        return status
    }
    async addComments({ request, response }){}
}

module.exports = StatusController

statusController showAll methods returns outputs like this:(comments array is empty where it shouldn’t be)

[
{
"id": 1,
"user_id": "2",
"status": "Hello Everyone ...",
"created_at": "2019-04-09 17:05:01",
"updated_at": "2019-04-09 17:05:01",
"comments": []
}
]
0 Likes

#3

Still doesn’t work :disappointed_relieved:

0 Likes

#4

Is there any status linked to a comment at all?

Status.comments().fetch() should give you all the comments linked to a status

1 Like

#5

Status.comments().fetch() produces error calling Status.comments is not a function :disappointed_relieved:

and yes … there is status linked to comments of different users.

0 Likes

#6

Can you please provide migration for us please?

0 Likes

#7

Status Migration

  up () {
    this.create('statuses', (table) => {
      table.increments()
      table.string('user_id')
      table.string('status')
      table.timestamps()
    })
  }

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

Comment Migration

  up () {
    this.create('comments', (table) => {
      table.increments()
      table.string('user_id')
      table.string('status_id')
      table.string('comment')
      table.timestamps()
    })
  }

  down () {
    this.drop('comments')
  }
}```
0 Likes

#8

I think you set the key in wrong order.
Please try :

class Status extends Model {
    comments() {
        return this.hasMany('App/Models/Comment', 'status_id', 'id')
    }
}
1 Like

#9

Yeah I tried that …
Still not wroking … producing empty comments array like as it was first :frowning:

0 Likes

#10

Sorry, but you have defined it correctly. I just saw your migrations below.
Can you share your table content also?

0 Likes

#11

I’m quite not sure exactly what did you mean by ‘table content’, please let me know more specifically what you need

0 Likes

#12

The SQL insert data, I would like to reproduce your issue in here

0 Likes

#14

comments table:

id -> 1
user_id -> 2
status_id -> 1
comment -> Hello Brother ... How're yaa 

statuses table:

id -> 1
user_id -> 1
status -> Hello Everyone ...
0 Likes

#15

Thats because you specified your status id as string in comments table, where the model defines it as integer. So it won’t match. Please change to integer the one in comments table

It should be working fine after you made changes :
42

3 Likes

#16

Yeah, that’s the mistake I made but was not concerned!

Thank You very much for your time :slight_smile: Have a nice day sir :slight_smile:

0 Likes

#17

Glad it’s fixed now! Have a nice day also :smiley:

0 Likes