0

I am working through chapter 10 of the Hartl book. In the conclusion of this chapter, we reset the heroku database and then migrate. However when I run:

heroku run rails db:migrate

I get an error:

rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedColumn: ERROR:  column "password_digest_string" of relation "users" does not exist
: ALTER TABLE "users" DROP "password_digest_string"

Earlier in the tutorial, I removed a column called password_digest_string because it was named incorrectly and I didn't need it yet. It seems like rails is trying to delete the column again even though it isn't there anymore. I deleted the migration file that removed this column but it still is trying to drop it. What's also odd is that I've migrated the database several times since dropping that column and never had this issue until I reset it. Any suggestions?

EDIT:

here is my schema file:

ActiveRecord::Schema.define(version: 20170121224748) do 

  create_table "users", force: :cascade do |t| 
    t.string   "name" 
    t.string   "email" 
    t.datetime "created_at",                      null: false 
    t.datetime "updated_at",                      null: false 
    t.string   "password_digest" 
    t.string   "remember_digest" 
    t.boolean  "admin",           default: false 
    t.index ["email"], name: "index_users_on_email", unique: true 
  end 

end

EDIT:

And my migrations in time-stamp order.

..._create_users.rb

    class CreateUsers < ActiveRecord::Migration[5.0]
      def change
        create_table :users do |t|
          t.string :name
          t.string :email 

          t.timestamps
        end
      end
    end

..._add_index_to_users_email.rb

    class AddIndexToUsersEmail < ActiveRecord::Migration[5.0]
      def change
        add_index :users, :email, unique: true
      end
    end

..._add_password_digest_to_users.rb

    class AddPasswordDigestToUsers < ActiveRecord::Migration[5.0]
      def change
        add_column :users, :password_digest, :string
      end
    end

..._remove_columns.rb

    class RemoveColumns < ActiveRecord::Migration[5.0]
      def self.up
        remove_column :users, :password_digest_string
      end
    end

..._add_remember_digest_to_users.rb

    class AddRememberDigestToUsers < ActiveRecord::Migration[5.0]
      def change
        add_column :users, :remember_digest, :string
      end
    end

..._add_admin_to_users.rb

    class AddAdminToUsers < ActiveRecord::Migration[5.0]
      def change
        add_column :users, :admin, :boolean, default: false
      end
    end
C-RAD
  • 1,052
  • 9
  • 18

2 Answers2

0

It looks to me that the AddPasswordDigestToUsers migration initially looked like this (probably you misspelled):

class AddPasswordDigestToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :password_digest_string
  end
end

Then you realized the column should be just password_digest. So you manually edited the migration file to what it looks now, rolled back, created the next migration which is RemoveColumns and then ran migration.

Now when you the migrations from the beginning, there will be no error if you are using sqlite3 (which is what you use for dev environment) due to the way column removal is handled in sqlite. But there will be an error if you use Postgres.

What you should have done

Instead of what you did, you should have just created a migration to change the column name, like this:

class RenamePasswordDigestStringColumn < ActiveRecord::Migration[5.0]
  def self.up
    rename_column :users, :password_digest_string, :password_string
  end
end

What you can do now

To get past the problem without doing many changes now, you can just delete the RemoveColumns migration (and commit, push... etc that). That would be OK and I don't think it will harm your production or development environments.

Community
  • 1
  • 1
Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
  • Awesome. Thank you. I think the issue I was having was that I was trying to delete the RemoveColumns migration before rolling back to a previous migration. Got it up and running on heroku now. Thanks for your help! – C-RAD Jan 23 '17 at 06:03
-1

$> heroku pg:reset DATABASE will make the db:migrate on heroku clean again

Harry
  • 1
  • Regardless whether this solves the error in the question or not, resetting the database destroys all data. This is not what people should use to solve their migration issues especially in production! – Tamer Shlash Jan 22 '17 at 03:10
  • Ya, I've tried that, I even created a new heroku app and tried to migrate to it. Are the migration files solely responsible for deciding how the tables are built? It seems like something else in my app is telling PG to delete the column even though it is already gone. The problem is confined to heroku, works fine on SQLite3 locally. – C-RAD Jan 22 '17 at 03:14
  • @TamerShlash do you have any suggestions for me? I think I created a migration to remove the unwanted column, and then rolled back the migration that created the column. Now I can't get postgres to stop trying the delete the column it didn't know about in the first place. – C-RAD Jan 23 '17 at 03:28
  • @C-RAD Yeah I think that's the case. If you have your app on github, you can give us the link so that we can see how the full migration history looks like. – Tamer Shlash Jan 23 '17 at 03:41
  • @TamerShlash Sounds good, it's https://github.com/crajcan/sample-app. I think if I can find the name of the initial migration that made the unwanted column, and then add it back I should be okay. I've been grepping all the branches to try to find it. – C-RAD Jan 23 '17 at 03:48