【问题标题】:How i can make this query using lucid models/query builder in adonis?我如何在 adonis 中使用 Lucid 模型/查询生成器进行此查询?
【发布时间】:2019-11-12 13:18:53
【问题描述】:

我需要进行一个查询,返回一本书的所有 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()
    })
  }

【问题讨论】:

    标签: node.js adonis.js


    【解决方案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
    
    }
    

    【讨论】:

      【解决方案2】:

      已编辑:此版本不使用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()
      })
      
      

      如果您有任何问题,请不要犹豫

      【讨论】:

      • 从 "book_unit_question" 中选择 count(*) 作为 "total",其中 "book_id" = $1 - 列 "book_id" 不存在
      • 问题在很多方面对吗?我尝试: book_unit_questions() { return this.manyThrough('App/Models/BookUnit', 'book_unit_questions', 'id', 'id') } 但我得到了同样的错误
      • 你能分享你的ER图吗?还是给个转储?
      • 好的,谢谢,我稍后再试试。目前我做不到
      • 你的代码是正确的。我将代码放在索引方法中。当我输入 show() 时一切正常
      猜你喜欢
      • 2013-05-30
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      相关资源
      最近更新 更多