我已尝试过db:setup
db:rollback
db:drop
db:migrate VERSION="0"
几乎我能想到的一切,但我无法弄清楚这个错误。我甚至尝试在初始化create方法之前运行它并删除表。即使我创建了一个新产品,就像产品数据库一样,它告诉我它在那里,但它告诉我,当我试图放弃它时它不存在。我花了两天时间浏览论坛和视频,找出发生了什么。这种情况发生在sqlite和mysql上。
- 已修改为包含迁移类 -
class CreateTablenames < ActiveRecord::Migration
def change
create_table :tablenames do |t|
t.string :ID
t.string :col1
t.string :col2
t.string :col3
t.timestamps null: false
end
end
end
- 结束编辑
E:\RailsApplications\Blog>rake db:migrate
== 20151130191336 CreateTablenames: migrating =================================
-- create_table(:tablenames)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: ID: CREATE TABLE "tablenames" ("id
" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "ID" varchar, "col1" varchar, "col
2" varchar, "col3" varchar, "created_at" datetime NOT NULL, "updated_at" datetim
e NOT NULL) E:/RailsApplications/Blog/db/migrate/20151130191336_create_tablename
s.rb:3:in `change'
C:in `migrate'
ActiveRecord::StatementInvalid: SQLite3::SQLException: duplicate column name: ID
: CREATE TABLE "tablenames" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "I
D" varchar, "col1" varchar, "col2" varchar, "col3" varchar, "created_at" datetim
e NOT NULL, "updated_at" datetime NOT NULL)
E:/RailsApplications/Blog/db/migrate/20151130191336_create_tablenames.rb:3:in `c
hange'
C:in `migrate'
SQLite3::SQLException: duplicate column name: ID
E:/RailsApplications/Blog/db/migrate/20151130191336_create_tablenames.rb:3:in `c
hange'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
E:\RailsApplications\Blog>rake db:migrate
== 20151130191336 CreateTablenames: migrating =================================
-- drop_table(:tablenames)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: no such table: tablenames: DROP TABLE "tablenames"E:/Rail
sApplications/Blog/db/migrate/20151130191336_create_tablenames.rb:3:in `change'
C:in `migrate'
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: tablenames
: DROP TABLE "tablenames"
E:/RailsApplications/Blog/db/migrate/20151130191336_create_tablenames.rb:3:in `c
hange'
C:in `migrate'
SQLite3::SQLException: no such table: tablenames
E:/RailsApplications/Blog/db/migrate/20151130191336_create_tablenames.rb:3:in `c
hange'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
答案 0 :(得分:2)
您正在尝试创建一个包含两列名为id
的表:
ActiveRecord::StatementInvalid: SQLite3::SQLException: duplicate column name: ID
: CREATE TABLE "tablenames" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "I
D" varchar, ...
默认情况下,我们已为rails创建了id
,然后您尝试添加ID
类型的另一个varchar
。
您需要将ID
重命名为其他内容,或者您需要告诉rails不要创建其id
列:
create table :tablenames, id: false do |t|
t.string :ID
... rest of your migration
end
理想情况下,只需删除重复的ID列并让Rails完成它。