Загрузка отношения `ownToMany` с использованием метода` with`, всегда возвращающего пустой массив - PullRequest
1 голос
/ 20 октября 2019

У меня есть 2 модели, Person и PersonType с отношением belongsToMany.

Проблема в том, что когда я пытаюсь получить все PersonType s относится к Person с использованием метода query().with(), он всегда возвращает пустой массив, даже если у меня есть правильные данные в 3 таблицах people, person_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"
           }
        ]
    }
]

Ниже приведены модели и миграции баз данных:

Person:

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

ЧтоЯ делаю не так?

Ответы [ 2 ]

1 голос
/ 29 октября 2019

Использование имени сводной таблицы в методе сопоставления и обработки формата ответа с помощью 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 голосов
/ 29 октября 2019

Я обнаружил проблему.

Поскольку я использовал knexSnakeCaseMappers из Objection.js, как предлагалось здесь , это делало Adonis неспособным выполнять некоторые операции, такие как отношения нагрузки.

Просто удалите его и используйте snake_case или попробуйте другое решение.

...