4

I have this class:

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')
  }
}

I need to change the data type of the column correct_answer_description to text. If i change my actually up() method to:

table.text('correct_answer_description')

and make a: adonis migration:refresh

All table is recreated and i lose the data in this table.

How i can only change the datatype without lose the data?

I try something like:

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

And make a:

adonis migration:run

But i get:

Nothing to migrate

bla
  • 667
  • 4
  • 19

1 Answers1

7

You need to create a new migration file like :

adonis make:migration ...

Type modification (new migration file) : .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();
    })
  }
}

and run pending migrations :

> adonis migration:run
crbast
  • 1,750
  • 1
  • 7
  • 16
  • 1
    if column type ```enu``` then I try to change the value of this column then how do this without losing data? if try above solution then give me error ```{ error: syntax error at or near "check" ``` – Amit Kadivar Nov 13 '19 at 05:25
  • I have no idea. But it might help you : https://stackoverflow.com/questions/39714345/update-enum-column-types-in-knex-migration – crbast Nov 13 '19 at 10:05