1

我有 2 个模型,PersonPersonTypebelongsToMany关系。

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

人员类型:

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

我做错了什么?

4

2 回答 2

1

在关联方法中使用数据透视表名称并使用 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
于 2019-10-29T12:44:58.283 回答
0

我发现了这个问题。

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

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

于 2019-10-29T13:48:43.853 回答