使用 `with` 方法加载 `belongsToMany` 关系总是返回一个空数组

Loading `belongsToMany` relation using the `with` method always returning an empty array

我有 2 个模型,PersonPersonType 具有 belongsToMany 关系。

问题是,当我尝试使用 query().with() 方法获取与 Person 相关的所有 PersonType 时,它总是 returning 一个空数组,即使我在 3 tables peopleperson_type 和中间 person_person_type 中有正确的数据。

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

const Person = use('App/Models/Person')

const people = await Person.query().with('types')

这是我从上面的代码中得到的:

[
    {
      "id": 3,
      "firstName": "Eleandro",
      "lastName": "Duzentos",
      "types": []
    },
    {
      "id": 5,
      "firstName": "Lorem",
      "lastName": "Ipsum",
      "types": []
    }
]

这是应该的return:

[
    {
      "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"
           }
        ]
    }
]

下面是每个模型及其数据库迁移:

人:

'use strict'

const Model = use('Model')

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

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

module.exports = Person
'use strict'

/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')

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

      table.string('first_name')
      table.string('last_name')
      table.date('birth_date')
      table.string('address')
      table.string('identification_number').nullable()

      table.integer('naturality_id').unsigned().index()
      table
        .foreign('naturality_id')
        .references('id')
        .inTable('municipalities')
        .onDelete('cascade')

      table.integer('person_genre_id').unsigned().index()
      table
        .foreign('person_genre_id')
        .references('id')
        .inTable('person_genres')
        .onDelete('cascade')

      table.boolean('is_activated').default(false).notNullable()

      table.timestamps()

      table.timestamp('deleted_at').nullable()
    })
  }

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

module.exports = CreatePeopleSchema

人物类型:

'use strict'

const Model = use('Model')

class PersonType extends Model {
  people() {
    return this.belongsToMany('App/Models/Person')
      .withTimestamps()
  }
}

module.exports = PersonType
'use strict'

/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')

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

      table.string('name').unique()
      table.string('slug').unique().index()
      table.text('description').nullable()
      table.string('icon').nullable()

      table.timestamps()
    })
  }

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

module.exports = CreatePersonTypesSchema

和中间 table:

'use strict'

/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')

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

      table.integer('person_id').unsigned().index()
      table
        .foreign('person_id')
        .references('id')
        .inTable('people')
        .onDelete('cascade')

      table.integer('person_type_id').unsigned().index()
      table
        .foreign('person_type_id')
        .references('id')
        .inTable('person_types')
        .onDelete('cascade')

      table.timestamps()
    })
  }

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

module.exports = PersonPersonTypeSchema

这是 Ludic 正在执行的查询:

select `person_types`.*, `person_person_type`.`person_type_id` as `pivot_person_type_id`, `person_person_type`.`person_id` as `pivot_person_id` from `person_types` inner join `person_person_type` on `person_types`.`id` = `person_person_type`.`person_type_id` where `person_person_type`.`person_id` in (?, ?)

我做错了什么?

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

'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

我发现了问题。

由于我按照建议 here 使用 Objection.js 中的 knexSnakeCaseMappers,因此 Adonis 无法执行某些操作,例如加载关系。

只需删除它并使用 snake_case 或尝试其他解决方案。