2

I've been developing a website on localhost and it wors fine. This morning, I've tried to push it to heroku using the command "git push heroku master" and then "heroku run rake db:migrate". When I try to do the second one, I have an error:

Connecting to database specified by DATABASE_URL
Migrating to DeviseCreateUsers (20130427200347)
Migrating to CreateAuthentications (20130427210108)
Migrating to AddTokenToAuth (20130427233400)
Migrating to AddNotificationToAuth (20130427234836)
Migrating to AddNotifToUser (20130428031013)
Migrating to AddDeviseToUsers (20130712103048)
==  AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:

PGError: ERROR:  column "email" of relation "users" already exists
: ALTER TABLE "users" ADD COLUMN "email" character varying(255) DEFAULT '' NOT N
sql_adapter.rb:652:in `async_exec'

I have found someone who had the same issue (heroku PGError: already exists 500 We're sorry, but something went wrong) but in my case the migration "AddDeviseToUsers" is not in the 'db/migrate' folder.

Previous migration affecting users table are :

class DeviseCreateUsers < ActiveRecord::Migration
  def change
  create_table(:users) do |t|
  ## Database authenticatable
  t.string :email,              :null => false, :default => ""
  t.string :encrypted_password, :null => false, :default => ""

  ## Recoverable
  t.string   :reset_password_token
  t.datetime :reset_password_sent_at

  ## Rememberable
  t.datetime :remember_created_at

  ## Trackable
  t.integer  :sign_in_count, :default => 0
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at
  t.string   :current_sign_in_ip
  t.string   :last_sign_in_ip

  ## Token authenticatable
  # t.string :authentication_token


  t.timestamps
end

add_index :users, :email,                :unique => true
add_index :users, :reset_password_token, :unique => true
end
end

and

class AddNotifToUser < ActiveRecord::Migration
 def change
  add_column :users, :notif, :string
 end
end

Thanks in advance !!

Edit - answering comments when I run : heroku run rake db:migrate:status

up     20130427200347  Devise create users
up     20130427210108  Create authentications
up     20130427233400  Add token to auth
up     20130427234836  Add notification to auth
up     20130428031013  Add notif to user
down    20130712103048  Add devise to users
down    20130719091217  Create relationships
.
.
.
Community
  • 1
  • 1
titibouboul
  • 1,348
  • 3
  • 16
  • 30
  • The error is self explanatory. You are trying to create a column on your table twice. – MurifoX Aug 20 '13 at 18:56
  • yes, but I have no access to the migration file called 'AddDeviseToUsers' ie. it's not in the '/db/migrate' directory !! – titibouboul Aug 20 '13 at 19:17
  • is it the first time you've deployed it to Heroku? – John Beynon Aug 20 '13 at 20:42
  • yes, it's the first time – titibouboul Aug 20 '13 at 20:55
  • what do you see when you run rake db:migrate:status – Anchor Aug 20 '13 at 23:36
  • @Anchor, I've answered in the post – titibouboul Aug 21 '13 at 00:07
  • The second migration `20130712103048 Add devise to users` seems a bit suspicious since the migration `20130427200347 Devise create users` will create the Users table including email. Can you post a GIST of the contents of that migration? If you were looking to enhance the Users table you should remove the email column from the second migration. – memoht Aug 21 '13 at 03:05
  • That's the thing, I don't know where to find that migration. I've done a search for "20130427200347" in the files of my project and I wasn't able to find something. – titibouboul Aug 21 '13 at 12:56

2 Answers2

8

I found the answer at Devise migration on existing model.

What I did was to comment out this line in DeviseCreateUsers migration:

"t.string :email,              :null => false, :default => """ 
Community
  • 1
  • 1
titibouboul
  • 1,348
  • 3
  • 16
  • 30
0

This issue is caused when one has a User model with email field. The solution is not to comment out the important line where it enforces having value for email even though it is an empty string.

To fix the issue, one need to manually change the email column as following (I only keep the relevant parts):

class AddDeviseToUsers < ActiveRecord::Migration[7.0]
  def self.up
    change_table :users do |t|
      # t.string :email,              null: false, default: ''
      t.change_null :email, false
      t.change_default :email, ''

      t.string :encrypted_password, null: false, default: ''
    end
  end

  def self.down
    change_table :users do |t|
      t.change_null :email, true
      t.change_default :email, nil

      t.remove :encrypted_password, null: false, default: ''
    end
  end
end