How i can alter a column type without lose the database data in migration using adonis?
Asked Answered
G

1

9

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

Gnosticize answered 11/11, 2019 at 13:0 Comment(0)
O
15

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
Odel answered 11/11, 2019 at 16:46 Comment(2)
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" Kythera
I have no idea. But it might help you : #39714845Odel

© 2022 - 2024 — McMap. All rights reserved.