SQLite3::SQLException: duplicate column name while migrating
try migrating your DB to version=0
with command: rake db:migrate VERSION=0
and then run rake db:migrate
Devise rake db:migrate fails because of duplicate column in my User's table - Rails 3.1
Try rake db:rollback
and then try again. When you did it the first time it added the id column. and why are you adding id :integer not null, primary key
it's automatic in rails. It should look like this:
class CreateProducts < ActiveRecord::Migration
def up
create_table :products do |t|
t.string :email
t.text :f_name
t.timestamps
end
end
def down
drop_table :products
end
end
You can get more information here http://guides.rubyonrails.org/migrations.html Duplicate Column name in rails
in the second migration take out the line add_column :articles, :description, :text
. You already created a description
column when you created the articles
table in the first migration, with the line t.text :description
:
create_table :articles do |t|
t.string :title
t.text :description
t.timestamps null: false
end
That's the duplicate column Rails is referring to in the error. Next time before you run migrations you can check schema.rb
file to see what your database tables look likeMissed this the first time, but you also are duplicating your timestamp columns, created_at
and updated_at
So the second migration is unnecessary
Migrations are cancelled and duplicate column error
As the exception suggests, you have already addedSQLite3::SQLException: duplicate column name: activation_digest: ALTER TABLE "users" ADD "activation_digest" varchar
activation_digest
in your users
table.You can check the columns of your users
table right from the rails console
with User.column_names
.
AddUserIdToMytimes .. Error - SQLite3::SQLException: duplicate column name: user_id: ALTER TABLE mytimes ADD user_id integer
Seems like your Mytime
model already have user_id
field.
What could you do? Remove this migration as unnecessary?
Duplicate column name error when running migration
That happens if you migrations are not in sync with the database schema. This could happen if
- you modified the database schema "by hand"
- you changed a migration file being run
- migrations have not been updated in the
schema_migrations
table
rake db:reset
would re-run all migrations from scratch. Otherwise you have to make the conflicting migration recognized as already-run by adding to the schema_migrations
table.See RailsGuides of migrations as well.
Rails 4 Migration Error (Can't rake db:migrate) SQLite3::SQLException: duplicate column name
Deleting a migration file doesn't really rollback the change it made in your database. Your best bet is to:
- Don't delete the migration but comment out the content of the migration class, and run
rake db:migrate
,
class AddEmailSentToNeeds < ActiveRecord::Migration
def change
add_column :needs, :email_sent, :boolean ,default: false
end
end
Just comment out the method but leave the class, so it would be:class AddEmailSentToNeeds < ActiveRecord::Migration
# def change
# add_column :needs, :email_sent, :boolean ,default: false
# end
end
This is just a hacky way to tell rails to skip this migration.OR
- start from the start so go do,
rake db:drop
,rake db:create
, andrake db:migrate
Rails database migration fails with duplicate column name: email when adding devise's generated User table
So the problem is you already have the users table inside your database. You need to either drop that table and create a new one if it's not working or just continue using that table.
It's not a good idea to drop your database but since you are making a new app(as you creating a users table) so in your case it'll be better to drop that database and recreate it(to make sure everything works). Try these commands in sequence:
rake db:drop
rake db:create
rake db:migrate
It should work fine and you need not do rails generate devise User
as you already have your users migration fileUpdate:
If you don't want to drop your database then you can create a new migration file and delete your users table there.
rails generate migration DropUsersTable
after that edit your migration fileclass DropUsersTable < ActiveRecord::Migration
def change
drop_table :users
end
end
and then do rake db:migrate
Related Topics
Gem Install Rmagick Fails on Os X El Capitan
Understanding The Behaviour of Inject Used with a Lambda in Ruby
Modifying Text Inside HTML Nodes - Nokogiri
Using Google Search Rest API in Ruby
Convert Durations in Ruby - Hh:Mm:Ss.Sss to Milliseconds and Vice Versa
Shellwords.Shellescape Implementation for Ruby 1.8
Rake Cucumber and Rake Spec Always Use "Develop" Environment
Get All Products of Category and Child Categories (Rails, Awesome_Nested_Set)
Issue Installing Gems on Windows 7 with Proxy
How to Find Out What Is Intercepting 'Method_Missing'
Regex to Check Alphanumeric String in Ruby
How Does The "#Map(&Proc)" Idiom Work When Introspecting Module Classes
Error: Spawn Enoent: Missing The SASS Executable
Current Password Can't Be Blank When Updating Devise Account
How to Access a Toplevel Entity in Ruby, from Inside a Module Which Defines The Same Name
Use Pry in Gems Without Modifying The Gemfile or Using 'Require'