我有这门课:
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')
}
}
Run Code Online (Sandbox Code Playgroud)
我需要将列的数据类型更改correct_answer_description为text. 如果我将实际up()方法更改为:
table.text('correct_answer_description')
Run Code Online (Sandbox Code Playgroud)
并做一个: adonis migration:refresh
所有表都重新创建,我丢失了该表中的数据。
我如何只更改数据类型而不会丢失数据?
我尝试类似:
this.alter('book_unit_question', (table) => {
table.text('correct_answer_description')
})
Run Code Online (Sandbox Code Playgroud)
并做一个:
adonis migration:run
Run Code Online (Sandbox Code Playgroud)
但我得到:
没有什么可迁移的
您需要创建一个新的迁移文件,例如:
类型修改(新迁移文件): .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();
})
}
}
Run Code Online (Sandbox Code Playgroud)
并运行挂起的迁移:
> adonis migration:run
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4506 次 |
| 最近记录: |