我需要进行查询,以返回一本书的所有book_unit_questions。
所以,我有Book.Id。
我正在尝试类似的事情:
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)
但是这种方式是从其他书籍中返回ID,而我也应该得到一些ID 1:
我的迁移文件:
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()
})
}
答案 0 :(得分:1)
manyThrough()
控制器:
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)
迁移文件:
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()
})
如有任何疑问,请不要犹豫
答案 1 :(得分:1)
经过一番尝试后,此代码可用于我的目的:
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
}