Adonis的“ belongsToMany”关系不起作用

时间:2019-10-21 11:28:47

标签: orm adonis.js lucid

我有以下型号:

ServiceCategory:

'use strict'

const Model = use('Model')

class ServiceCategory extends Model {
  services() {
    return this
      .belongsToMany('App/Models/Service')
      .withTimestamps()
  }
}

module.exports = ServiceCategory
'use strict'

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

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

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

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

      table.timestamps()

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

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

module.exports = ServiceCategorySchema

服务:

'use strict'

const Model = use('Model')

class Service extends Model {
  categories() {
    return this
      .belongsToMany('App/Models/ServiceCategory')
      .withTimestamps()
  }
}

module.exports = Service
'use strict'

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

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

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

      table.float('price', 10, 2)

      table.boolean('is_negotiable').default(true)

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

      table.timestamps()

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

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

module.exports = ServiceSchema

问题是,当我尝试使用其所有 Service 服务获得所有 ServiceCategories 时,我只会获得带有空服务数组的 ServiceCategories

const serviceCategories = await ServiceCategory
      .query()
      .with('services')
      .fetch()
[
    {
      "id": 1,
      "name": "Beleza",
      "slug": "beleza",
      "description": null,
      "icon": null,
      "isActivated": true,
      "createdAt": "2019-10-21T10:43:32.000Z",
      "updatedAt": "2019-10-21T10:43:32.000Z",
      "services": []
    }
]

中间表:

'use strict'

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

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

      table.integer('service_id').unsigned().index()
      table
        .foreign('service_id')
        .references('id')
        .inTable('services')
        .onDelete('cascade')

      table.integer('service_category_id').unsigned().index()
      table
        .foreign('service_category_id')
        .references('id')
        .inTable('service_categories')
        .onDelete('cascade')

      table.timestamps()
    })
  }

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

module.exports = ServiceServiceCategorySchema

但是我可以将service附加到service_category上:

const service = await Service.create(params)

await service.categories().attach([serviceCategoryId])

这成功将service关联到数据透视表上的service category

现在,为什么我可以添加关系但无法加载它?

1 个答案:

答案 0 :(得分:1)

'use strict'

const Model = use('Model')

class ServiceCategory extends Model {
  services() {
    return this
      .belongsToMany('App/Models/Service')
      .pivotTable("service_service_category")
      .withTimestamps()
  }
}

module.exports = ServiceCategory

添加数据透视表名称后,请尝试使用此功能