1
I have the following schema:
Book:
class BookSchema extends Schema {
up () {
this.create('books', (table) => {
table.increments()
table.string('description')
table.timestamps()
})
}
Bookunit:
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()
table.string('description')
table.integer('user_id').references('id').inTable('users')
table.timestamps()
table.unique(['unit', 'sequence', 'book_id'])
})
}
Bookunitquestion
class BookUnitQuestionSchema extends Schema {
up () {
this.create('book_unit_question', (table) => {
table.increments()
table.integer('book_unit_id').references('id').inTable('book_unit')
table.string('question_form')
table.string('option_form')
table.string('type_answer')
table.string('description')
table.string('correct_answer_description')
table.integer('correct_answer_description_id')
table.text('image_sound')
table.boolean('status').defaultTo(false)
table.integer('user_id').references('id').inTable('users')
table.timestamps()
})
}
I need to perform a query that will return me all the questions of a given book. I tried it that way:
const Book = use('App/Models/Book')
const book = await Book.find(2) //Get book from id
await book.load('book_unit_questions') // Load book_unit_questions
const questions = book.getRelated('book_unit_questions') // Return questions
return questions
Then I have the following json result:
[ {"id":1,"book_unit_id":2,"description":null,"created_at":null,"updated_at":null},
{"id":2,"book_unit_id":5,"description":454,"created_at":null,"updated_at":null}
]
However, I need more information about book_unit_id, such as his Description.
I already have the book_unit_id being returned, as I can add more fields in my query?
My Book Models:
user () {
return this.belongsTo('App/Models/User')
.select('id', 'username')
}
book_units(){
return this.hasMany('App/Models/BookUnit')
}
book_unit_questions() {
return this.manyThrough('App/Models/BookUnit', 'book_unit_questions')
}
My Bookunit Models:
book_unit_questions() {
return this.hasMany('App/Models/BookUnitQuestion')
}
book () {
return this.belongsTo('App/Models/Book')
.select('id', 'description as descricaounidade')
}
user () {
return this.belongsTo('App/Models/User')
.select('id', 'username')
}
My bookUnit Question models:
static get table () {
return 'book_unit_question'
}
user () {
return this.belongsTo('App/Models/User')
.select('id', 'username')
}
book_units(){
return this.belongsTo('App/Models/BookUnitQuestion')
}
books () {
return this.manyThrough('App/Models/Book', 'book_unit_questions', 'id', 'book_unit_id')
}