Orm 使用`with`方法加载`belongTomany`关系时始终返回空数组

Orm 使用`with`方法加载`belongTomany`关系时始终返回空数组,orm,adonis.js,lucid,Orm,Adonis.js,Lucid,我有两个模型,Person和PersonType,具有归属关系 问题是,当我尝试使用query()方法获取与Person相关的所有PersonType时,with()方法总是返回空数组,即使我在3个表people、Person\u type和intermediatePerson\u-type中有正确的数据 这是试图加载人员类型的代码: const Person=use('App/Models/Person')) const people=await Person.query()。带有('type

我有两个模型,PersonPersonType,具有
归属关系

问题是,当我尝试使用
query()
方法获取与Person相关的所有PersonType时,with()
方法总是返回空数组,即使我在3个表
people
Person\u type
和intermediate
Person\u-type
中有正确的数据

这是试图加载人员类型的代码:

const Person=use('App/Models/Person'))
const people=await Person.query()。带有('types')
这是我从上面的代码中得到的:

[
    {
      "id": 3,
      "firstName": "Eleandro",
      "lastName": "Duzentos",
      "types": []
    },
    {
      "id": 5,
      "firstName": "Lorem",
      "lastName": "Ipsum",
      "types": []
    }
]
这是它应该返回的:

[
    {
      "id": 3,
      "firstName": "Eleandro",
      "lastName": "Duzentos",
      "types": [
          {
              name: "Requester",
              slug: "requester"
          },
          {
              "name": "Provider",
              "slug": "provider"
          },
      ]
    },
    {
      "id": 5,
      "firstName": "Lorem",
      "lastName": "Ipsum",
      "types": [
           {
              "name": "Requester",
              "slug": "requester"
           }
        ]
    }
]
以下是每种型号及其数据库迁移:

人:

“严格使用”
常量模型=使用('Model')
类人扩展模型{
静态get表(){
返回“人民”
}
类型(){
返回此.belongToMany('App/Models/PersonType')
.withTimestamps()
}
}
module.exports=个人
“严格使用”
/**@type{import('@adonisjs/lucid/src/Schema')}*/
const Schema=use('Schema')
类CreatePeopleSchema扩展了模式{
向上(){
这个.create('people',(table)=>{
表3.1(a)
table.string('first_name')
table.string('last_name')
表.日期(“出生日期”)
table.string('地址')
table.string('identification_number').nullable()
table.integer('naturality_id')。unsigned().index()
桌子
.外国(“自然身份”)
.references('id'))
.inTable(“市政当局”)
.onDelete('级联')
table.integer('person\u-id')。unsigned().index()
桌子
.foreign('person\u type\u id')
.references('id'))
.inTable('person\u tyres')
.onDelete('级联')
table.boolean('is_activated')。默认值(false)。notNullable()
表1.timestamps()
表.timestamp('deleted_at').nullable()
})
}
向下(){
这个。删除('人')
}
}
module.exports=CreatePeopleSchema
个人类型:

“严格使用”
常量模型=使用('Model')
类PersonType扩展了模型{
人(){
返回此.belongTomany('App/Models/Person'))
.withTimestamps()
}
}
module.exports=PersonType
“严格使用”
/**@type{import('@adonisjs/lucid/src/Schema')}*/
const Schema=use('Schema')
类CreatePersonTypesSchema扩展了模式{
up(){
创建('person\u types',(table)=>{
表3.1(a)
table.string('name').unique()
table.string('slug').unique().index()
table.text('description').nullable()
table.string('icon').nullable()
表1.timestamps()
})
}
向下(){
this.drop('person\u types')
}
}
module.exports=CreatePersonTypesSchema
和中间表:

“严格使用”
/**@type{import('@adonisjs/lucid/src/Schema')}*/
const Schema=use('Schema')
类PersonPersonTypeSchema扩展了Schema{
向上(){
创建('person\u person\u type',(表)=>{
表3.1(a)
table.integer('person_id')。unsigned().index()
桌子
.外国人(“个人身份证”)
.references('id'))
.inTable('人')
.onDelete('级联')
table.integer('person\u type\u id')。unsigned().index()
桌子
.foreign('person\u type\u id')
.references('id'))
.inTable('人员类型')
.onDelete('级联')
表1.timestamps()
})
}
向下(){
this.drop('person\u person\u type')
}
}
module.exports=PersonPersonTypeSchema
这是Ludic正在执行的查询:

选择'person'u types`..'person'u person'u type`.'person'u type'id`作为'pivot'u person'u type'id`.'person'u type`.'person'u id`作为'pivot'u person'u id`从'person'u types'中的'person'u type'内部连接'person'u person'u person'u type`.'id`.'person'u type`.'person'u id`.'

我做错了什么?

在关联方法中使用pivot表名,并使用toJSON()处理响应格式

我发现了问题

由于我按照建议使用了objective.js中的
knexSnakeCaseMappers
,这使得Adonis无法执行一些操作,比如加载关系

只需将其移除,然后使用
snake\u case
或尝试其他解决方案

'use strict'

const Model = use('Model')

class Person extends Model {
  static get table () {
    return 'people'
  }

  types() {
    return this.belongsToMany('App/Models/PersonType')
      .pivotTable('person_type')
  }
}

module.exports = Person