Orm Adonis'belongsToMany'关系不起作用

Orm Adonis'belongsToMany'关系不起作用,orm,adonis.js,lucid,Orm,Adonis.js,Lucid,我有以下型号: 服务类别: “严格使用” 常量模型=使用('Model') 类ServiceCategory扩展模型{ 服务(){ 还这个 .belongtomany(“应用程序/模型/服务”) .withTimestamps() } } module.exports=服务类别 “严格使用” /**@type{import('@adonisjs/lucid/src/Schema')}*/ const Schema=use('Schema') 类ServiceCategorySchema扩展了模

我有以下型号:

服务类别:

“严格使用”
常量模型=使用('Model')
类ServiceCategory扩展模型{
服务(){
还这个
.belongtomany(“应用程序/模型/服务”)
.withTimestamps()
}
}
module.exports=服务类别
“严格使用”
/**@type{import('@adonisjs/lucid/src/Schema')}*/
const Schema=use('Schema')
类ServiceCategorySchema扩展了模式{
向上(){
创建('service_categories',(table)=>{
表3.1(a)
table.string('name')
表.string('slug').index()
table.text('description').nullable()
table.string('icon').nullable()
table.boolean('is_activated')。默认值(false)。notNullable()。默认值(true)
表1.timestamps()
表.timestamp('deleted_at').nullable()
})
}
向下(){
this.drop('service\u categories')
}
}
module.exports=ServiceCategorySchema
服务:

“严格使用”
常量模型=使用('Model')
类服务扩展模型{
类别(){
还这个
.belongtomany('应用程序/模型/服务类别')
.withTimestamps()
}
}
module.exports=服务
“严格使用”
/**@type{import('@adonisjs/lucid/src/Schema')}*/
const Schema=use('Schema')
类ServiceSchema扩展了模式{
向上(){
创建('services',(表)=>{
表3.1(a)
table.string('name')
表.string('slug').index()
table.text('description').nullable()
table.string('icon').nullable()
表.浮动('价格',10,2)
table.boolean('is_propossible')。默认值(true)
table.boolean('is_activated')。默认值(false)。notNullable()。默认值(true)
表1.timestamps()
表.timestamp('deleted_at').nullable()
})
}
向下(){
this.drop('services'))
}
}
module.exports=ServiceSchema
问题是,当我尝试使用其服务获取所有服务类别时,我只会获取带有空服务数组的服务类别:

const serviceCategories=等待服务类别
.query()
.with('服务')
.fetch()
[
{
“id”:1,
“姓名”:“Beleza”,
“slug”:“beleza”,
“描述”:空,
“图标”:空,
“已激活”:正确,
“createdAt”:“2019-10-21T10:43:32.000Z”,
“更新日期”:“2019-10-21T10:43:32.000Z”,
“服务”:[]
}
]
中间表:

“严格使用”
/**@type{import('@adonisjs/lucid/src/Schema')}*/
const Schema=use('Schema')
类ServiceCategorySchema扩展了模式{
向上(){
创建('service\u service\u category',(表)=>{
表3.1(a)
table.integer('service_id')。unsigned().index()
桌子
.foreign('service_id'))
.references('id'))
.inTable(“服务”)
.onDelete('级联')
table.integer('service\u category\u id')。unsigned().index()
桌子
.foreign(“服务类别id”)
.references('id'))
.inTable(“服务类别”)
.onDelete('级联')
表1.timestamps()
})
}
向下(){
this.drop('service\u service\u category')
}
}
module.exports=ServiceServiceCategorySchema
但是我可以将
服务
附加到
服务类别

const service=wait service.create(参数)
等待服务.categories().attach([serviceCategoryId])
这将成功地将
服务
关联到透视表上的
服务类别

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

'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
添加数据透视表名称后,如果有效,请尝试使用此名称


添加数据透视表名称后,如果正在进行迁移,请尝试使用此名称file@AmitPatel,我已经添加了迁移文件,用于放置迁移file@AmitPatel,我已经添加了迁移文件