Error during migration while setting up Devise - ruby-on-rails

I'm relatively new to Rails and I'm running into an issue while trying to set up Devise. I believe the issue stems from the fact that I already generated a user scaffold before trying to install Devise, yet I don't know how to solve this problem. When I proceed through the Devise setup, I get to the step where I have to enter the following code:
rails generate devise User
That works, and I get this back from terminal:
invoke active_record
create db/migrate/20120609032448_add_devise_to_users.rb
insert app/models/user.rb
route devise_for :users
The next step is to migrate the database, which I attempt but get the following error:
== AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar(255) DEFAULT '' NOT NULL
Tasks: TOP => db:migrate
I've tried destroying the original User scaffold along with the original User migration but I keep getting stuck at this error. Help would be much appreciated!

Your new migration probably has an email column defined in it. Comment that line regarding adding an email column out and run it again. It's likely you've already got an email column in your model.

Try going to this file
db/migrate/20120609032448_add_devise_to_users.rb
and in the code where it says
change_table(:users)...
alter this to
create_table(:users)...

Related

ruby migration rolled back but get error when run migration again

This is the error I get when running a migration in a RoR application:
PG::Error: ERROR: column "bulk_bill" of relation "questionnaires" already exists
A little background:
I rolled back a migration so that I could change the default setting for a column.
Once I ran the migration again I got the error above.
I can see in the postgresql table in development that the column does exist. I have a data in the tables and in the bulk_bill column it has it's default set to false.
What are the recommended steps I need to take so that I so the migration can be run successfully.
I am a beginner in ruby and find the ruby documentation still a little hard to follow.
def up
add_column :questionnaires, :bulk_bill, :boolean, :default => false
end
def down
remove_column :pnp_questionnaires, :bulk_bill
end
In your up method you're creating column on questionaries table, and in your down method you are removing it from pnp_questionaries. Remove column does not raise an excepton if table doesn't exists, hence you have your problem.
Simplest solution:
Comment out add_column from up.
Run migrations (yes, run empty migration).
Fix your down method to remove questionaries table.
Rollback migration.
Uncomment your up method.
UPDATE:
My bad - point 3 was to be 'remove column from questionaries', not 'remove table'.
You need to rerun the migration which created the table (hopefuly you didn't alter it in a meantime). Go to the given migration, comment out down method body, and run rake db:migrate:redo VERSION=xxxxxxx where xxxxxx is the timestamp in this migration filename.

Fixing error in deleted migration and table

I'm trying to update my heroku database. The problem: I created an old model & table that we no longer need. That table had previously been migrated to a production server (along with other changes). I did a destroy of the model using:
rails destroy model ReallyLongModelName
That also deleted the migration that created the table.
Later, I created a migration to drop that table.
class Drop_ReallyLongTableName < ActiveRecord::Migration
def change
drop_table :really_long_table_name
end
end
I'm now getting a couple of errors.
First error:
When trying to migrate the database to the production version of the application, I get this error.
Input string is longer than NAMEDATALEN-1 (63)
I'm not sure how to go back and edit the name to avoid the long name, so that it clears
Second error:
When trying to rollback the Drop_ReallyLongTableName migration, its aborts the rake because
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: no such table: really_long_table_name
Does anyone have any ideas on how to solve this? Thanks!
Have you tried running this from your Terminal/console:
heroku run console
ActiveRecord::Migration.drop_table(:table_name)
The migration is the way to go but this might be worth a try to see if the table even exists any more (since your second error message seems to indicate that it is no longer there).
Also, and for what it's worth, you can add conditional logic to your DB migrations should you need to in the future
def change
drop_table :table_name if self.table_exists?("table_name")
end

Rails and Devise giving Error for Signup Page

I've googled and SOed for the answer to this, so even though this question is similar to others posed already, please know I have tried all of the answers posted here already.
I've been following a tutorial to set up a Rails site with users etc. I'm at the stage where I tried to use Devise to generate a user db.
I get this error when I try to access the signup page:
ActiveRecord::StatementInvalid in Devise::RegistrationsController#new
The advice I have found is to use rake db:migrate in order to fix the db.
This gives me the following error:
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "installs" ADD "email" varchar(255) DEFAULT '' NOT NULL/ruby/testapp/omrails/db/migrate/20130510151217_add_devise_to_installs.rb:5:in `block in up'
/ruby/testapp/omrails/db/migrate/20130510151217_add_devise_to_installs.rb:3:in `up'
/Users/BWS/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
/Users/BWS/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate
The interwebs then leads me to believe that deleting my development.sqlite3 and rerunning rake db:migrate will solve my problem, however this leads to:
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "installs" ADD "email" varchar(255) DEFAULT '' NOT NULL/ruby/testapp/omrails/db/migrate/20130510151217_add_devise_to_installs.rb:5:in `block in up'
again.
[Possibly Related]
I also receive the following message when I do just about anything with rails:
/Users/BWS/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
But all of the suggested advice online has yet to fix the issue.
Sorry for the long post, trying to be very thorough, any advice would be appreciated.
Update:
So I can't get it to generate a new schema (I trashed the DB folder several times to try different things) but here is one of the deleted ones:
# encoding: UTF-8
# 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 to check this file into your version control system.
ActiveRecord::Schema.define(:version => 0) do
end
It looks like you have two migrations that both adds an email field to installs. You have to look through all you migration files and see if this is true. There are three ways to add a email field to a table, so look for more then one of these:
#1
create_table "installs" do |t|
t.string :email
#2
change_table "installs" do |t|
t.string :email
#3
add_column :installs, :email, :string
Some versions of Devise create migrations without the .rb extension. When I had the same error as you I navigated back to the 'migrate' folder and sure enough the generated migration lacked the extension. Adding .rb and then running $ rake db:migrate fixed this issue for me.

Rails SQLite3 duplicate column - Treehouse Tutorial

I've just started a Treehouse tutorial working on a Simple Ruby App. To give context, I previously had to delete and start over on this project, which I deleted the main app folder and started over.
Following the tutorial we've created a User database which includes all Devise included tables (:email, :name, etc). The error that I'm getting states that I have a duplicated column name. After reviewing and tinkering I tried to comment out the problem column to see if things will move forward in my migration but it just gives the same error for the next line.
A few times I've tried to drop the database, re-create and re-migrate but I'm getting the same issue.
Ideally I'd like to just remove the databases and recreate them, but that doesn't seem like something I can do.
Here is the error:
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar(255) DEFAULT '' NOT NULL
/Users/pbj/Desktop/code/rails/treebook/db/migrate/20121130035155_add_devise_to_users.rb:5:in `block in up'
/Users/pbj/Desktop/code/rails/treebook/db/migrate/20121130035155_add_devise_to_users.rb:3:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
When I generated the User set from Devise it also gave me the create_status.rb, devise_create_user.rb and add_devise_to_users.rb (which is the problematic file). The thought of removing Devise and starting over crossed my mind, but I've already done that which has brought me to this new error.
Apologize if this is somewhat rudimentary for all the experience programmers, but as mentioned above, I'm learning but just got off of the original path of this tutorial.
Any and all help is appreciated.
I think despite of your db/migrate/0000-00000_create_user.rb you have added another add_email_to_users migration to your model. So check your create_user.rb in db/migration folder and see if email column is already there, then check for other migration file such as add_email_to_users.rb in db/migrate folder, if you found such an extra add_email migration then delete it.
At the end rename/delete your development.sqlite3 file and try to rake db:migrate from scratch.
this might help you solve the problem.
I'm taking the same course and I think the error lies that Ruby 4 just came out and it has a duplicated line on the migrate directory, I deleted and then ran rake db:migrate. It worked perfectly.
Your error message said this:
duplicate column name: email: ALTER TABLE "users" ADD "email" varchar(255) DEFAULT '' NOT NULL
Your migration file (some random number then...)_add_devise_to_users.rb is adding these lines which you should now comment out
# t.string :email, null: false, default: ""
# add_index :users, :email, unique: true
This will allow you to now migrate with this command
rake db:migrate
If you are still struggling and don't mind destroying and recreating the database you can do this to start fresh
rake db:drop db:create db:migrate
Hope this helps!

Authlogic causing all rails tests to fail

I'm building an app in Rails 3 using Authlogic for authentication. I have a User model with a database table and a user_session model without one
All of my tests fail, whether I run
Error:
test_the_truth(UsersControllerTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table:
user_sessions: DELETE FROM "user_sessions" WHERE 1=1
It's expecting user_session to have a table even though it inherits from Authlogic. Does anybody know how to fix this?
I was having the same problem and this took me a while to discover... the thing is Authlogic has no table in the database. When we create the sessions with Rails generate, this is creating also automatically a fixture, which of course will fail later since there is no table to fill data in. Solution: delete the fixture of user_sessions.
Read more about the problem here
Ensure you have defined properly test database in config/database.yml, then try rake db:test:prepare or rake db:migrate RAILS_ENV=test.

Resources