[英]How i can make this query using lucid models/query builder in adonis?
I need to make a query that return all book_unit_questions of one Book.我需要进行一个查询,返回一本书的所有 book_unit_questions。
So, i have the Book.Id.所以,我有 Book.Id。
I'm trying something like:我正在尝试类似的东西:
SELECT BO.id,
BUQ.description
FROM book_unit_question BUQ
JOIN book_unit BU
ON(BUQ.book_unit_id = BU.book_id)
INNER JOIN Books BO
ON(BU.book_id = 1)
But this way is returning id from other books, and i was expected some the id 1:但是这种方式是从其他书中返回 id,而我预计会有一些 id 1:
My migrations files:我的迁移文件:
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()
table.integer('sequence').notNullable()
table.string('description')
table.integer('qt_question')
table.boolean('status').defaultTo(false)
table.integer('user_id').references('id').inTable('users')
table.timestamps()
table.unique(['unit', 'sequence', 'book_id'])
})
}
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()
})
}
manyThrough()
编辑:此版本不使用manyThrough()
Controller: Controller:
const BookUnit = use('App/Models/BookUnit')
const Question = use('App/Models/BookUnitQuestion')
const bIds = await BookUnit.query().where('book_id', 1).ids() //Get all bookUnit ids
const questions = await Question.query().whereIn('book_unit_id', bIds).fetch()
return questions
const questions = await Question.query().whereIn('book_unit_id', bIds).paginate(2, 2)
Migration files:迁移文件:
this.create('books', (table) => {
table.increments()
table.string('code').notNullable()
table.string('description').notNullable()
table.timestamps()
})
this.create('book_units', (table) => {
table.increments()
table.integer('book_id').unsigned().references('id').inTable('books')
table.integer('unit')
table.timestamps()
})
this.create('book_unit_questions', (table) => {
table.increments()
table.integer('book_unit_id').unsigned().references('id').inTable('book_units')
table.integer('description')
table.timestamps()
})
Don't hesitate if you have any questions如果您有任何问题,请不要犹豫
After some tentatives, this code works for my purpose:经过一些试探,这段代码适用于我的目的:
async show(request){
const bookQuestions = await BookUnitQuestion
.query()
.with('book_unit')
.with('user')
.with('book', (builder) => {
builder.where('id', request.params.id)
})
.paginate()
return bookQuestions
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.