Can not rollback or reset AdonisJs migrations


#1

I have 2 migration files related to this issue:

class PersonsSchema extends Schema {
  up () {
    this.create('persons', (table) => {
      table.string('name', 30).notNullable().primary()
      table.timestamps()
    })
  }

  down () {
    this.drop('persons')
  }
}

the second one is created to alter persons table generated by the above code:

class PersonsSchema extends Schema {
  up () {
    this.table('persons', (table) => {
      table.dropPrimary()
      table.dropColumn('name')
      table.dropTimestamps()
      table.string('ssn', 30).primary()
      table.string('lname', 30).notNullable()
      table.string('fname', 30).notNullable()
      table.string('country_name', 30).notNullable().references('name').inTable('countries')
    })
  }

  down () {
    this.table('persons', (table) => {
      table.dropPrimary()
      table.dropColumn('ssn')
      table.dropForeign()
      table.dropColumn('country_name')
      table.dropColumns('lname', 'fname')
      table.string('name', 30).primary()
      table.timestamps()
    })
  }
}

The migration runs: adonis migration:run but if I want to rollback (in case you add the second file above after running the first schema) or if I want to reset (in case you clone the repository and thos both above schema files exist at the beginning) I get this error:
I can not reset or rollback a simple migration because I am getting this error message:

 "Can't DROP 'persons__foreign'; check that column/key exists"

You can clone my demo repository and follow the instruction in the demo file if you want to reproduce it.

I investigated the dropForeign() function and checked some pure MySQL related messages but can not figure out what is wrong with this.

Persons table is here:

mysql> describe persons;                                                                                                                         
describe persons;                                                                                                                                
+--------------+-------------+------+-----+---------+-------+                                                                                    
| Field        | Type        | Null | Key | Default | Extra |                                                                                    
+--------------+-------------+------+-----+---------+-------+                                                                                    
| ssn          | varchar(30) | NO   | PRI | NULL    |       |                                                                                    
| lname        | varchar(30) | NO   |     | NULL    |       |                                                                                    
| fname        | varchar(30) | NO   |     | NULL    |       |                                                                                    
| country_name | varchar(30) | NO   | MUL | NULL    |       |                                                                                    
+--------------+-------------+------+-----+---------+-------+ 

Countries table is here:

describe countries;                                                                                                                              
+------------+------------------+------+-----+---------+-------+                                                                                 
| Field      | Type             | Null | Key | Default | Extra |                                                                                 
+------------+------------------+------+-----+---------+-------+                                                                                 
| name       | varchar(30)      | NO   | PRI | NULL    |       |                                                                                 
| population | int(10) unsigned | NO   |     | NULL    |       |                                                                                 
+------------+------------------+------+-----+---------+-------+ 

And show create table persons gives me this:

| persons | CREATE TABLE `persons` (                                                                                                             
  `lname` varchar(30) NOT NULL,                                                                                                                  
  `fname` varchar(30) NOT NULL,                                                                                                                  
  `country_name` varchar(30) NOT NULL,                                                                                                           
  `name` varchar(30) DEFAULT NULL,                                                                                                               
  `created_at` datetime DEFAULT NULL,                                                                                                            
  `updated_at` datetime DEFAULT NULL,                                                                                                            
  KEY `persons_country_name_foreign` (`country_name`),                                                                                           
  CONSTRAINT `persons_country_name_foreign` FOREIGN KEY (`country_name`) REFERENCES `countries` (`name`)                                         
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |  

What am I missing?

Billal BEGUERADJ


#2

I found the solution, it is self explanatory:

down () {                                                                     
    this.table('persons', (table) => {                                          
      table.dropForeign('country_name')                                         
      table.dropColumn('country_name')                                          
      table.dropPrimary()                                                       
      table.dropColumn('ssn')                                                   
      table.dropColumns('lname', 'fname')                                       
      table.string('name', 30).notNullable().primary()                          
      table.timestamps()                                                        
    })                                                                          
  }