0
votes

I am following Michael Hartl's book and I am getting this error when I run the server:

enter image description here

I am getting ActiveRecord::PendingMigrationError when I run the server, this shows up:

Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development

Please I've been stuck in this error for so long.

When I type $ RAILS_ENV=development rake db:migrate I get this error:

== 20161209073230 AddActivationToUsers: migrating ============================= -- add_column(:users, :activation_digest, :string) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: activation_digest: ALTER TABLE "users" ADD "activation_digest" varchar (required)>' Tasks: TOP => db:migrate (See full trace by running task with --trace)

test/mailers/previews/user_mailer_preview.rb

UserMailerPreview < ActionMailer::Preview

# Preview this email at http://localhost:3000/rails/mailers/user_mailer/account_activation
def account_activation user = User.first user.activation_token = User.new_token UserMailer.account_activation(user) end

# Preview this email at http://localhost:3000/rails/mailers/user_mailer/password_reset def password_reset UserMailer.password_reset end

end

Schema.rb:

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20161123005710) 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.string   "activation_digest"
    t.index ["email"], name: "index_users_on_email", unique: true   end

end

Latest migration is db/migrate/[timestamp]_add_activation_to_users.rb:

class AddActivationToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :activation_digest, :string
    add_column :users, :activated, :boolean, default: falserao
    add_column :users, :activated_at, :datetime
  end
end
5

5 Answers

1
votes

The correct command to apply unapplied migrations is RAILS_ENV=development rake db:migrate

0
votes

This just means that you have a migration pending. When you create a new migration

railas g migration MigrationName

it means that it changes something in the database schema or the layout of your database. In order to commit that change, you have to run:

bin/rails db:migrate

This will look into your migration file and apply it to your databse. Once the migration is done, you can run server as usual again.

rails server

If you have more question, I recommend reading the migration documentation that Rails published:

http://guides.rubyonrails.org/active_record_migrations.html

0
votes

SQLite3::SQLException: duplicate column name: activation_digest:

(The following is the SQL command that actually modifies the db):

ALTER TABLE "users" ADD "activation_digest"

The SQL command reads like plain English. Somehow one of your migrations is doing something a previous migration already did, namely adding the activation_digest column to your users table in the db. If you look in the directory db/migrate/, you will see all your migration files. If you open one of them up, you should sort of be able to tell what it is doing. So look at all your migration files and find the two migrations that both add the activation_digest column. If the two migration files are identical, then you need to delete one--but take the following steps before deleting a migration:

https://www.baserails.com/questions/i-messed-up-while-generating-my-migration-how-can-i-undo-it

Also check out the section Rolling Back in the Rails Guide:

http://edgeguides.rubyonrails.org/active_record_migrations.html#reverting-previous-migrations

If for some reason you don't have two identical migration files that both add the activation_digest column, e.g one of the migration files does something additionally, then you need to figure out what steps in the tutorial that you did wrong, then rollback to the last migration that you know is correct. Finally, follow the steps in the tutorial again for generating the subsequent migrations.

0
votes

It seems your users table already have a column named activation_digest. Remove the add_column line where the column is added from the migration file and run the migration again.

I think this migration file should work:

class AddActivationToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :activated, :boolean, default: false
    add_column :users, :activated_at, :datetime
  end
end
0
votes

Ok the answer is very simple!Just try migrating your DB to version=0 with command: rake db:migrate VERSION=0

and then run rake db:migrate