ruby - SQLite3::SQLException: duplicate column name: User_id: ALTER TABLE "comments" ADD "User_id" integer/ -
i ran command
rails g migration adduser_idtocomments user_id:string
and figured out user_id should integer , ran
rails g migration adduser_idtocomments user_id:integer --force
thinking overwrite initial command.
but now, i'm getting error:
``` louismorin$ rake db:migrate == 20140910155248 addindextocomments: migrating =============================== -- add_column(:comments, :index, :string) -> 0.0069s == 20140910155248 addindextocomments: migrated (0.0070s) ======================
== 20140910181022 adduseridtocomments: migrating ============================== -- add_column(:comments, :user_id, :integer) rake aborted! standarderror: error has occurred, , later migrations canceled:
sqlite3::sqlexception: duplicate column name: user_id: alter table "comments" add "user_id" integer/users/louismorin/code/cp299/db/migrate/20140910181022_add_user_id_to_comments.rb:3:in change' activerecord::statementinvalid: sqlite3::sqlexception: duplicate column name: user_id: alter table "comments" add "user_id" integer /users/louismorin/code/cp299/db/migrate/20140910181022_add_user_id_to_comments.rb:3:in
change' sqlite3::sqlexception: duplicate column name: user_id /users/louismorin/code/cp299/db/migrate/20140910181022_add_user_id_to_comments.rb:3:in `change' tasks: top => db:migrate (see full trace running task --trace) ```
here's schema.rb file
``` activerecord::schema.define(version: 20140910155210) do
create_table "comments", force: true |t| t.text "body" t.integer "post_id" t.datetime "created_at" t.datetime "updated_at" t.string "user_id" end add_index "comments", ["post_id"], name: "index_comments_on_post_id" create_table "posts", force: true |t| t.string "title" t.text "body" t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" t.integer "topic_id" end add_index "posts", ["topic_id"], name: "index_posts_on_topic_id" add_index "posts", ["user_id"], name: "index_posts_on_user_id" create_table "topics", force: true |t| t.string "name" t.boolean "public", default: true t.text "description" t.datetime "created_at" t.datetime "updated_at" end create_table "users", force: true |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" 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" t.string "confirmation_token" t.datetime "confirmed_at" t.datetime "confirmation_sent_at" t.string "unconfirmed_email" t.string "name" t.datetime "created_at" t.datetime "updated_at" t.string "role" t.string "avatar" t.string "image" end add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end
```
because of error, second migration -- changing column type -- didn't run. if migration intended change 1 column, delete file generated , try again.
if don't yet have data in column care about, that's pretty easy:
rails g migration changetypeofuseridoncomments
this migration name isn't special. might have been
dowhateveriwant
then, edit created migration's change method this:
def change remove_column :comments, :user_id add_column :comments, :user_id, :integer add_index :comments, :user_id end
when run un-run migrations rake db:migrate
, shouldn't trip on 1 errored (because deleted it), , should run one, removes column , adds correct type.
if have data want save, procedure more complicated. change method have grab current user_id's each comment, , assign them new comments when create new column. below should work:
def change user_ids = {} comment.find_each{ |c| user_ids[c.id] = c.user_id.to_i } remove_column :comments, :user_id add_column :comments, :user_id, :integer add_index :comments, :user_id comment.each{ |c| c.update_attribute(:user_id, user_ids[c.id]) end
also note names of migration in command camelcase or snake_case. so: addcolumnuseridtocomments
or add_column_user_id_to_comments
. naming did might cause problems.
** edit **
modify column's type in sqlite3
it appears sqlite has no way modify/drop column. i'd suggest either:
dropping , restarting sqlite table
drop table, drop original user_id migration , line in new 1 removing old user_id column, , create new table new migrations. should work fine if don't care data
switching postgres, database heroku uses.
it's idea (because want production , local databases behave identically), can bug-prone switch to.
look here guidance -- change sqlite postgresql in fresh rails project
Comments
Post a Comment