Adonis `belongsToMany` 关系不起作用
Adonis `belongsToMany` relation not working
我有以下型号:
服务类别:
'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
问题是,当我尝试获取所有 ServiceCategories 及其 Services 时,我只获取 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": []
}
]
中级 table:
'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
关联到枢轴 table 上的 service category
。
现在,为什么我可以添加关系但无法加载它?
'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
在添加 数据透视表 名称后,如果可以的话,试试这个
我有以下型号:
服务类别:
'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
问题是,当我尝试获取所有 ServiceCategories 及其 Services 时,我只获取 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": []
}
]
中级 table:
'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
关联到枢轴 table 上的 service category
。
现在,为什么我可以添加关系但无法加载它?
'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
在添加 数据透视表 名称后,如果可以的话,试试这个