failing migrations : PG::ProtocolViolation Rails - ruby-on-rails

I just updated to rails4 and realized that my db roles and database was somehow not setup.
So I dropped the db and re-created the app_development db and the roles.
Now I am trying to run rake db:migrate and its failing with following error:
== DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
-> 0.0081s
-- add_index(:users, :email, {:unique=>true})
-> 0.0031s
-- add_index(:users, :reset_password_token, {:unique=>true})
-> 0.0030s
== DeviseCreateUsers: migrated (0.0144s) =====================================
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::ProtocolViolation: ERROR: bind message supplies 1 parameters, but prepared statement "a1" requires 0
: INSERT INTO "schema_migrations" ("version") VALUES ('20130815235601')
Tasks: TOP => db:migrate
I tried to run migrations for different tables manually and it gives the same for each table.
Here is a sample migration file that it is complaining about:
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
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
## Token authenticatable
# t.string :authentication_token
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
end
I think one way is to delete the migration and re-create it.
But I am not sure of exact steps to do that.
Can anyone help me with exact steps with getting the migrations running. I dont mind dropping the db etc since the project is dev stage and there is not much prod data/schema I need to worry about
Thanks.
Rails 4.0
PG 9.3.0
Update1:
Dont see any rows in schema_mgrations:
select * from schema_migrations;
version
---------
(0 rows)
Update2:
Inserting by hand the same values works fine !
INSERT INTO "schema_migrations" ("version") VALUES ('20130815235601');
INSERT 0 1
select * from schema_migrations
version
----------------
20130815235601
(1 row)
Update 3
I ran migrations for different table manually and its failing for each table, so its not an issue with any specific migration.
I also tried to revert back to Rails 3.2.14 and am still facing the same issue.
Feeling lost here and stuck in it for 4-5 days now !!

Related

issues with installing devise

here's my current user schema:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# first_name :string
# last_name :string
# created_at :datetime not null
# updated_at :datetime not null
#
require 'elasticsearch/model'
class User < ActiveRecord::Base
searchkick word_start: [:user]
has_many :posts
validates :first_name, :last_name, presence: true
end
these are the steps I'm able to go through before the problem rears its head:
rails generate devise:install
rails generate devise user
rake db:migrate
once i try to migrate it, this is what comes up:
== 20160707230510 AddDeviseToUsers: migrating =================================
-- change_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DuplicateColumn: ERROR: column "email" of relation "users" already exists
: ALTER TABLE "users" ADD "email" character varying DEFAULT '' NOT NULL
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:5:in `block in up'
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:3:in `up'
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR: column "email" of relation "users" already exists
: ALTER TABLE "users" ADD "email" character varying DEFAULT '' NOT NULL
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:5:in `block in up'
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:3:in `up'
PG::DuplicateColumn: ERROR: column "email" of relation "users" already exists
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:5:in `block in up'
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:3:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
as you can tell from my earlier user schema, there is no email column there. so ... why does this error come up?
**EDIT - should have posted devise migration file - the one that doesn't work **
class AddDeviseToUsers < ActiveRecord::Migration
def self.up
change_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, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.inet :current_sign_in_ip
t.inet :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
# Uncomment below if timestamps were not included in your original model.
# t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
def self.down
# By default, we don't want to make any assumption about how to roll back a migration when your
# model already existed. Please edit below which fields you would like to remove in this migration.
raise ActiveRecord::IrreversibleMigration
end
end
EDIT AGAIN
when i run rails g devise user ... this is what comes up:
Running via Spring preloader in process 72318
invoke active_record
create db/migrate/20160707230510_add_devise_to_users.rb
insert app/models/user.rb
route devise_for :users
Turns out all I had to do was to do rake db:setup which recreates the db. Then I ran rake db:migrate, and no issues came up this time.
FYI. Hope this helps someone out there.
To eliminate migration errors on duplicate fields, use t.change as shown below.
t.change :email, :string, :null => false, :default => ""
Notice that for t.change to work, you have to specify the type for the field being changed. In the case of the email migration above, the email field was of type string.
You can refer at: devise wiki

How to fix SQLite Devise Error

I'm following a tutorial on adding the Devise gem to Rails. One feature of the gem is generating a "user" using Devise, for further user authentication (Facebook, Twitter, etc.). I'm running into the following error:
== 20150906025001 AddDeviseToUsers: migrating =================================
-- change_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar DEFAULT '' NOT
NULL/Users/jaker/.rvm/gems/ruby-2.0.0-p643/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize'
I already have a User model in my app, that has an email, so this makes sense. However, when I try to run a migration and delete my "User" table, I'm still getting the same error.
[timestamp]_add_devise_to_users.rb:
class AddDeviseToUsers < ActiveRecord::Migration
def self.up
change_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, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
# Uncomment below if timestamps were not included in your original model.
# t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
def self.down
# By default, we don't want to make any assumption about how to roll back a migration when your
# model already existed. Please edit below which fields you would like to remove in this migration.
raise ActiveRecord::IrreversibleMigration
end
end
Does anyone know how to fix this? I'm really confused, and no documentations have seemed to help. Thanks so much.
This error is happening because you already have a column called email in your User model
You could comment (or remove) the line:
t.string :email, null: false, default: ""
and the script will continue.

Rails 4 SQLite3::SQLException error

I have a devise User model which I want to add a admin boolean field so I ran
rails generate migration add_admin_to_users admin:boolean which created the following migration
class AddAdminToUsers < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean
end
end
However when I run rake db:migrate I keep getting the following error
SQLite3::SQLException: no such table: users: ALTER TABLE "users" ADD "admin" boolean/home/notebook/.rvm/gems/ruby-2.0.0-p353/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
I have tried to rake db:migrate VERSION=0 to rollback to the beginning and redone rake db:migrate again but I keep getting the same error
Here is my user model migration file
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, :null => false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
end
end
My db/schema.rb file is as follows:
ActiveRecord::Schema.define(version: 20140217093954) do
create_table "entities", force: true do |t|
t.string "entity"
t.string "genre"
t.string "url"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "entities", ["entity"], name: "index_entities_on_entity", unique: true
end
It sounds like your development database is not in sync with the schema (or what you think the schema is). Running database migrations is intended for incremental changes and not intended to be be a way to rollback the database and run from the first migration. For this you want to use rake db:reset, which will drop the database and load the schema from db/schema. This post has a good overview of the different database rake commands.
Your database has not users table, you need to create users table first:
rails g migration create_users
And put that migration before the one you are trying to run now, i.e. changing its timestamp.
An easier option is to add in your current file:
class AddAdminToUsers < ActiveRecord::Migration
def change
create_table :users
add_column :users, :admin, :boolean
end
end
Whatever of the solutions you apply, you have a problem with the order of your migrations, search inside migrate folder for a migration that actually creates users table.

rails generate devise User error when migrating

I'm a bit new in Rails world and I try to add a new field in devise schema.
I found this :
rails generate model NAME [field[:type][:index] field[:type]
and tried to apply the command :
rails generate devise User linkedin:string
The process seemed correct :
invoke active_record
create db/migrate/20130902085306_add_devise_to_users.rb
insert app/models/user.rb
route devise_for :users
But when I launch a db:migrate it occures an error :
PG::Error: ERROR: column "email" of relation "users" already exists
What did I do wrong ? why does it say (and is it related) email is wrong while it was ok before ?
Thanks a lot !
Here is the migration file result :
class AddDeviseToUsers < ActiveRecord::Migration
def self.up
change_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
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
## Token authenticatable
# t.string :authentication_token
t.string :linkedin
# Uncomment below if timestamps were not included in your original model.
# t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
def self.down
# By default, we don't want to make any assumption about how to roll back a migration when your
# model already existed. Please edit below which fields you would like to remove in this migration.
raise ActiveRecord::IrreversibleMigration
end
end
Does your users table already have email field? It seems there is, you have to remove creating an email field again by removing it from migration and make sure it is not referenced twice in user.rb model
db/migrate/20130902085306_add_devise_to_users.rb
Ok!
I've found what was wrong : I created a new table instead of updating existing one.
So the good task was :
rails g migration add_columnLinkedin_to_users
Then adding in the new created migration file :
change_table :users do |t|
t.string :linkedin
end
And db:migrate was a success !
Thanks for your helps !

PGError: ERROR: column "email" of relation "users" already exists

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
.
.
.
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 => """

Resources