0

I'm trying to create a self-relationship/subcategory using Adonisjs, but it returns the result []/null. is there any solution?

models

@column()
public categoryId: string

@hasMany(() => Category)
public subCategory: HasMany<typeof Category>

controllers

const page = request.qs('page', 1)
const limit = request.qs('limit', 10)
const categories = await Category.query()
    .whereNull('category_id')
    .preload('subCategory')
    .paginate(page, limit)

migrations

table.bigInteger('category_id').unsigned().references('categories.id').onDelete('cascade')
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Soelllll
  • 3
  • 2

1 Answers1

0

Need to Add ForeignKey in model

  @hasMany(() => Category, {
  foreignKey : 'category_id', // defaults to id
  })
  public subcategory: HasMany<typeof Category>

i hope this works