Adonis migration: nothing to migrate


#1

I want to remove some columns and add other ones in users table:

class UserSchema extends Schema {                                                                                                                
  up () {                                                                                                                                        
    this.create('users', (table) => {                                                                                                            
      table.increments()                                                                                                                         
      table.string('username', 80).notNullable().unique()                                                                                        
      table.string('email', 254).notNullable().unique()                                                                                          
      table.string('password', 60).notNullable()                                                                                                 
      table.timestamps()                                                                                                                         
      table.string('country', 30).notNullable()                                                                            
      table.dropColumn('username')        
      table.dropColumn('email')                                                                                                           
    })                                                                                                                                           
  }                                                                                                                                              
                                                                                                                                                 
  down () {                                                                                                                                      
    this.drop('users')                                                                                                                           
  }                                                                                                                                              
}   

When I run adonis migration:run I get: Nothing to migrate.
I just followed what is mentioned in the accepted answer to this question

But when I run adonis make:migration users then I choose Select table, then I run adonis migration:run it complains about the fact users table already exists.

What am I missing?


#2

You can’t dropcolumn in a create, that would happen in a select.

If you want to modify the existing users table, create a new migration that selects the users table.

In the up you’ll have a this.select() instead of this.create() … that’s where you would do your dropColumns and add the country field. Then in that migration’s down method you’d want to reverse that, re-add the fields you dropped and remove the field you added.

You actually have several problems here.

  1. You’re modifying the existing migration (incorrectly) and then trying to run migrations again without rolling it back
  2. You’ll have problems doing user auth without email or username

You have a couple options. If you’re still early in development, you could just migration:reset then modify the default users migration, and re-run the migration

Or, if you don’t want to reset the db, instead remove your changes to the user migration, then do the following:

adonis make:migration users --action select

Edit that new file thusly:

  up () {                                                                                                                                        
    this.select('users', (table) => {                                                                                                            
      table.string('country', 30).notNullable()                                                                            
      table.dropColumn('username')        
      table.dropColumn('email')                                                                                                           
    })                                                                                                                                           
  }                                                                                                                                              
                                                                                                                                                 
  down () {   
    this.select('users', (table) => {                                                                                                                                   
      table.dropColumn('country')
      table.string('username', 80).notNullable().unique()                                                                                        
      table.string('email', 254).notNullable().unique()       
    })
  }                                                                                                                                              

Then run migrations again

Each file represents an incremental change to the db schema. If you’re early in development it’s easy enough to rollback/reset the db change the existing migration, then run migrations again, but it’s good to get into the habit of instead creating a new migration that does the relevant table alters as that’s what you’re have to do in production.