Delete default value before removing column

#1

Hi everyone,

First api, dozen of migration files, playing up and down with “adonis migration:run” and “adonis migration:reset”.

I have a problem when I do “adonis migration:reset”, down() method is executed on each migration file and there are some of them which were made in order to add new columns.

This operation fails because I have a column with associated default value:

info:
{ number: 4922,
state: 9,
class: 16,
message: ‘ALTER TABLE DROP COLUMN active failed because one or more objects access this column.’,
serverName: ‘DESKTOP-MIFMVLP\SQLEXPRESS’,
procName: ‘’,
lineNumber: 1,
name: ‘ERROR’,
event: ‘errorMessage’ } },
name: ‘RequestError’,
precedingErrors:
[ { RequestError: The object ‘DF__hosts__active__795DFB40’ is dependent on column ‘active’.

I’ve seen there would be a method removing first this values, and then dropping each column but alterTable() function is not recognized in Schema:

https://knexjs.org/#Schema-alter

How can I drop columns with default value enabled?

0 Likes

#2

@gabrielm
try adjustment your foreign key add .references(‘id’).inTable(‘your_table’).onDelete(‘CASCADE’);
if not work`s maybe try .onDelete(‘SET NULL’);
https://knexjs.org/#Schema-foreign

look`s this my example:
table.foreign(‘ficha_escala_id’).references(‘ficha_escalas.id’).onDelete(‘CASCADE’);

0 Likes

#3

Thanks diogon01, I’m using onDelete(‘Cascade’) for foreign keys but in this case its not a foreign key problem, it’s a default value constraint wich has to be removed before dropping the column itself.

On creation, up() method looks like this:

up () {
    this.alter('mytable', (table) => {
      table.boolean('columnxxxxx').defaultTo(true)
      table.boolean('columnyyyyy').defaultTo(true)
    })
  }
0 Likes