【问题标题】:How i can alter a column type without lose the database data in migration using adonis?如何在使用 adonis 迁移时更改列类型而不会丢失数据库数据?
【发布时间】:2020-03-07 04:08:23
【问题描述】:

我有这门课:

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('correct_answer_description')
      table.boolean('status').defaultTo(false)
      table.integer('user_id').references('id').inTable('users')
      table.timestamps()
    })
  }

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

我需要将correct_answer_description 列的数据类型更改为text。 如果我将我的实际 up() 方法更改为:

table.text('correct_answer_description')

并制作一个:adonis migration:refresh

重新创建所有表,我丢失了该表中的数据。

我怎样才能只更改数据类型而不丢失数据?

我尝试类似:

this.alter('book_unit_question', (table) => {
  table.text('correct_answer_description')
})

然后做一个:

adonis migration:run

但我明白了:

没有什么要迁移的

【问题讨论】:

    标签: node.js adonis.js


    【解决方案1】:

    您需要创建一个新的迁移文件,例如:

    类型修改(新迁移文件):.alter()

    class BookUnitQuestionSchema extends Schema {
      up() {
        this.alter('book_unit_questions', (table) => {
          table.text('correct_answer_description').notNullable().alter();
        })
      }
    
      // reverse modification 
      down() {
        this.table('book_unit_questions', (table) => {
          table.string('correct_answer_description').notNullable().alter();
        })
      }
    }
    

    并运行挂起的迁移:

    > adonis migration:run
    

    【讨论】:

    • 如果列类型enu 然后我尝试更改此列的值,那么如何在不丢失数据的情况下执行此操作?如果尝试上述解决方案,则给我错误{ error: syntax error at or near "check"
    • 我不知道。但它可能会帮助你:stackoverflow.com/questions/39714345/…
    猜你喜欢
    • 2012-06-14
    • 1970-01-01
    • 2017-08-24
    • 2020-06-16
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    相关资源
    最近更新 更多