简体   繁体   中英

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?

I tested your code.

I noticed this problem:

By default the AdonisJS CLI creates a table with the name: book_units (you code: book_unit ) command: > adonis make:migration BookUnit

I solved the problem with ( App/Models/BookUnit ):

class BookUnit extends Model {
    static get table() {
        return 'book_unit'
    } // Model table name

    ...

OR

change

this.create('book_unit', (table) => {

by

this.create('book_units', (table) => {

Explanation

Models and migrations are not linked. If you change the name of the table in the migration, it's necessary to pass it on to the model.

Full code

schemas:

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()
    })
  }

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

//=====================================

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()
    })
  }

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

Models:

class BookUnit extends Model {
    static get table() {
        return 'book_unit'
    }

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

//==============================

class Book extends Model {
    book_units() {
        return this.hasMany('App/Models/BookUnit')
    }
}

Test:

var data = {
        "book_id": 1,
        "unit": 1,
        "sequence": 1,
        "description": "UNIT_01_GRAMMAR",
        "qt_question": 5,
        "status": false
    }
    const bookUnit = await BookUnit.create(data)

Result: 在此处输入图像描述

Test config:

  • SQLite

Don't hesitate if you need more information.

Try doing this

table.integer('book_id').unsigned().references('id').inTable('books').notNullable()

next thing in your api its better to fetch the book by its id and then use create method.

let book = await Book.findBy('id',params.id)//your book id from your req object
book.book_units().create({
    "book_id": 1,
    "unit": 1,
    "sequence": 1,
    "description": "UNIT_01_GRAMMAR",
    "qt_question": 5,
    "status": false
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM