Adonis migration to drop unique constraint

Viewed 509

I am working on an Adonis JS project. I want to do the equivalent of ALTER TABLE products DROP CONSTRAINT unique_col_id where the unique_col_id is responsible for products.sku to be unique.

I tried this in AdonisJS database/migrations/filename.js:

'use strict'

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

class ProductsAlterUniqueSkuSchema extends Schema {
  up () {
    this.alter('products', (table) => {
      // alter table
      table.integer('sku').unique(false);
    })
  }

  down () {
    this.table('products', (table) => {
      // reverse alternations
    })
  }
}

module.exports = ProductsAlterUniqueSkuSchema

But I get the error Error: Duplicate column name 'sku'. How do I tell AdonisJS migration script to alter the table schema to drop the constraint?

1 Answers

You need to drop uniqueIndex. You can't alter this using .alter().

So for your solution, this will be the code:

'use strict'

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

class ProductsAlterUniqueSkuSchema extends Schema {
    up () {
        this.alter('products', (table) => {
            // alter table
            table.dropUnique('sku')
        })
    }

    down () {
        this.table('products', (table) => {
            // reverse alternations (check your first migration, and just repeat)
            table.integer('sku').unique()
        })
    }
}

module.exports = ProductsAlterUniqueSkuSchema
Related