ruby-on-railsdatabasemigrationruby-on-rails-5rails-models

All table fields are not showing? (Rails/MySQL)


I've created new fields, "first_name", "last_name", "email", and "password" in the model generated migrate file as shown:

class CreateUsers < ActiveRecord::Migration[5.2]

   def up
     create_table :users do |t|
        #added fields
        t.string "first_name", :limit => 25
        t.string "last_name", :limit => 50
        t.string "email", :default => '', :null => false
        t.string "password", :limit => 40

        t.timestamps
   end
  end

  def down
    drop_table :users
  end

end

However, once I show the fields from, in this case "users"

mysql> SHOW FIELDS FROM users;

It returns this table as it should has, but it should have returned first_name, last_name, email, and password as well.

Furthermore, the schema.rb file also excludes these.

I have no idea why it is not implementing these fields. I'm also fairly new to Rails as well and do appreciate suggestions on title edits to better fit my situation here as I'm unsure whether the terminology is correct. Thank you!


Solution

  • From the description shared it seems like first you have migrated the table without the column and then you must have added the columns in the migration file.

    Thus, when you migrated the table to the database these columns were not present in the migration file thus you need to follow the below mentioned process to overcome this situation:

    1) check the migration status using

    rake db:migrate:status
    

    The create_users migration must be up having a version number

    2) Now make this migration down using:

    rake db:migrate:down VERSION="version_number_of_the_create_users_migration"
    

    3) Repeat Step 1 , the result should be that the create_users migration is now down.

    4) Now make this migration up using

    rake db:migrate #it will migration all the down migrations
    rake db:migrate:up VERSION="version_number_of_the_create_users_migration" #it will migrate the specific migration
    

    5) Now check the schema.rb and it should be having those fields.