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

问题描述 投票:1回答:2

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

问题是,当我尝试使用query().with()方法获取与Person相关的所有PersonType时,即使我在其中具有正确的数据,它也总是返回一个空数组。 3个表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": []
    }
]

这是它应该返回的内容:

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

PersonType:

'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

和中间表:

'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 (?, ?)

我做错了什么?

orm adonis.js lucid
2个回答
0
投票

在关联方法中使用数据透视表名称,并使用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

0
投票

我发现了问题。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.