【问题标题】:Adonis js Drop column when migration:rollbackAdonis js 迁移时删除列:回滚
【发布时间】:2020-06-30 15:17:22
【问题描述】:

我正在使用adonis js 制作网站。我需要通过迁移将某些列添加到现有表中,并在运行“adonis migration:rollback”时删除这些列。如何编写迁移 down() 函数?

我在下面提到的adonis make:migration medicines --action=select 的代码

'use strict'

/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')

class MedicinesSchema extends Schema {
  up () {
    this.alter('medicines', (table) => {
      // alter table
      table.boolean('front_page');
      table.integer( 'brand_id' ).nullable();
      table.integer('offer_id').nullable();

    })
  }

  down () {
    this.table('medicines', (table) => {
      // reverse alternations
      // HOW DO I WRITE THE REVERSALS HERE ?
    })
  }
}

module.exports = MedicinesSchema

提前致谢

【问题讨论】:

    标签: javascript adonis.js


    【解决方案1】:

    您可以使用dropColumn()

    官方文档示例:https://knexjs.org/#Schema-table

    knex.schema.table('users', function (table) {
      table.dropColumn('name');
      table.string('first_name');
      table.string('last_name');
    })
    

    输出:

    alter table `users` add `first_name` varchar(255), add `last_name` varchar(255);
    alter table `users` drop `name`
    

    阿多尼斯版本:

    down () {
       this.table('medicines', (table) => {
          table.dropColumn('myColumn');
       })
    }
    

    AdonisJS 使用Knex 进行数据库查询

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2020-12-01
      • 1970-01-01
      • 2015-08-05
      • 2015-12-07
      • 2021-11-25
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      相关资源
      最近更新 更多