我试图进行迁移,但在第一次尝试运行时错过了t.integer“subject_id”的一行。
迁移看起来像这样:
class CreatePages < ActiveRecord::Migration
def up
create_table :pages do |t|
t.integer "subject_id"
# same as: t.references :subject
t.string "name"
t.string "permalink"
t.integer "position"
t.boolean "visible", :default => false
t.timestamps
end
add_index("pages", "subject_id")
add_index("pages", "permalink")
end
def down
drop_table :pages
end
end
以上现在似乎是正确的,但当我再次尝试再次运行时,我得到了这个:
George$ rake db:migrate
== 20150110112705 CreatePages: migrating ======================================
-- create_table(:pages)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'pages' already exists: CREATE TABLE `pages` (`id` int(11) auto_increment PRIMARY KEY, `subject_id` int(11), `name` varchar(255), `permalink` varchar(255), `position` int(11), `visible` tinyint(1) DEFAULT 0, `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB/Users/George/Sites/simple_cms/db/migrate/20150110112705_create_pages.rb:4:in `up'
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'pages' already exists: CREATE TABLE `pages` (`id` int(11) auto_increment PRIMARY KEY, `subject_id` int(11), `name` varchar(255), `permalink` varchar(255), `position` int(11), `visible` tinyint(1) DEFAULT 0, `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB
/Users/George/Sites/simple_cms/db/migrate/20150110112705_create_pages.rb:4:in `up'
Mysql2::Error: Table 'pages' already exists
/Users/George/Sites/simple_cms/db/migrate/20150110112705_create_pages.rb:4:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
我明白这显然是在说你正在尝试创建一个名为Pages的表,这个表已经存在,因为我搞砸的行是在创建表之后立即执行的行。但我无法弄清楚如何使用已存在的表再次运行迁移。我试图注释掉该行以创建表格,但这也不起作用。
我还在Rails做一个初学者教程,所以上面是我唯一能想到的尝试。
编辑:我现在尝试了Shivam的建议,但现在得到以下内容:== 20150110112657 CreateSubjects: reverting ===================================
-- drop_table(:subjectsend)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Unknown table 'simple_cms_development.subjectsend': DROP TABLE `subjectsend`/Users/George/Sites/simple_cms/db/migrate/20150110112657_create_subjects.rb:13:in `down'
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown table 'simple_cms_development.subjectsend': DROP TABLE `subjectsend`
/Users/George/Sites/simple_cms/db/migrate/20150110112657_create_subjects.rb:13:in `down'
Mysql2::Error: Unknown table 'simple_cms_development.subjectsend'
/Users/George/Sites/simple_cms/db/migrate/20150110112657_create_subjects.rb:13:in `down'
Tasks: TOP => db:rollback
(See full trace by running task with --trace)
George$
答案 0 :(得分:2)
确保迁移文件与运行迁移期间的迁移文件完全相同,然后像这样回滚迁移:
rake db:rollback STEP=1 # rollbacks 1 step
完成后,相应地编辑文件并执行:
rake db:migrate
或者您可以通过以下方式具体说明该版本:
rake db:migrate VERSION=20141222070950 #verison is timestamp from your migration filename
<强>替代地强>
我可以看到你要做的就是在模型表中添加一个新列subject_id
。您也可以通过运行standalone migration来实现此目的:
rails generate migration AddSubjectIdToPages subject_id:integer
接下来是:
rake db:migrate