【发布时间】:2014-11-04 12:28:57
【问题描述】:
我运行了命令
rails g migration AddUser_idToComments User_id:string
然后我发现 User_id 应该是一个整数,所以我跑了
rails g migration AddUser_idToComments User_id:integer --force 认为它会覆盖初始命令。
但是现在,我收到了这个错误:
``` louismorin$ rake db:迁移 == 20140910155248 AddIndexToComments:迁移 =============================== -- add_column(:cmets, :Index, :string) -> 0.0069 秒 == 20140910155248 AddIndexToComments: 迁移 (0.0070s) ======================
== 20140910181022 AddUserIdToComments: 迁移 ============================== -- add_column(:cmets, :User_id, :integer) 耙中止! StandardError:发生错误,此迁移和所有后续迁移已取消:
SQLite3::SQLException: 重复列名:User_id: ALTER TABLE "cmets" ADD "User_id" integer/Users/louismorin/code/CP299/db/migrate/20140910181022_add_user_id_to_cmets.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:inchange'
SQLite3::SQLException:重复的列名:User_id
/Users/louismorin/code/CP299/db/migrate/20140910181022_add_user_id_to_cmets.rb:3:in `change'
任务:TOP => db:migrate
(通过使用 --trace 运行任务查看完整跟踪)
```
这是我的 schema.rb 文件
``` ActiveRecord::Schema.define(version: 20140910155210) 做
create_table "comments", force: true do |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 do |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 do |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 do |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
```
【问题讨论】:
-
SQLite 不直接支持更改列的类型。
-
Louis -- 你能在上面添加你的
db/schema.rb文件吗?查看您的数据库当前的样子会很有帮助。 -
是的。混合的 CamelCase_snake_case 迁移名称似乎混淆了 Rails,导致大写的列名称
User_id。只是要记住的一件事。您是否尝试过以下方法?