Monday, 4 November 2019

Relation does not exist in adonis/node.js

I have two tables: book and book_units as you can see below:

class BookSchema extends Schema {
  up () {
    this.create('books', (table) => {
      table.increments()
      table.string('code').notNullable().unique()
      table.string('description')
      table.string('authors')
      table.boolean('status').defaultTo(false)
      table.integer('user_id').references('id').inTable('users')
      table.timestamps()
    })
  }
}


class BookUnitSchema extends Schema {
  up () {
    this.create('book_unit', (table) => {
      table.increments()
      table.integer('book_id').references('id').inTable('books').notNullable()
      table.integer('unit').notNullable().unique()
      table.integer('sequence').unique()
      table.string('description')
      table.integer('qt_question')
      table.boolean('status').defaultTo(false)
      table.integer('user_id').references('id').inTable('users')
      table.timestamps()
    })
  }
}

In the Book model, i defined a relationship with book_units:

class Book extends Model {

    book_units () {
        return this.hasMany('App/Models/BookUnit')
    }

}

And in the Book Unit Model:

class BookUnit extends Model {

    book () {
        return this.belongsTo('App/Models/Book')
    }

}

I'm trying to make a insert using postman in the book_unit using this json:

{
    "book_id": 1,
    "unit": 1,
    "sequence": 1,
    "description": "UNIT_01_GRAMMAR",
    "qt_question": 5,
    "status": false
}

But i'm receiving:

insert into "book_units" ("book_id", "created_at", "description", "qt_question", "sequence", "status", "unit", "updated_at", "user_id") values ($1, $2, $3, $4, $5, $6, $7, $8, $9) returning "id" - relation "book_units" does not exist

This book with id 1 exist in database. Why i'm receiving this error?



from Relation does not exist in adonis/node.js

No comments:

Post a Comment