Ruby on Rails Mysql2::Error: Table 'pages' 已经存在:CREATE TABLE `pages` 但不能迁移回来 [英] Ruby on Rails Mysql2::Error: Table 'pages' already exists: CREATE TABLE `pages` but cannot migrate back
问题描述
我试图运行迁移,但在第一次尝试运行时错过了 t.integer "subject_id" 中的一行.
I was trying to run a migration but had missed a line out of t.integer "subject_id" when first trying to run it.
迁移如下所示:
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
以上现在看起来是正确的,但是当我再次尝试运行它时,我得到了这个:
The above is now seemingly correct but when I try to run it again I get this:
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 的表,该表已经存在,因为我搞砸的行是在创建表后立即出现的行.但是我无法弄清楚如何在表已经存在的情况下再次运行迁移.我试图注释掉创建表格的行,但这也不起作用.
I understand it is obviously saying you are trying to create a table called Pages which already exists as the line I messed up is the one immiediately after the table was created. But I cannot figure out how to have the migration run again with the table already existing. I have tried to comment out the line to create the table but that does not work either.
我还在做一个 Rails 初学者教程,所以上面是我唯一能想到的尝试.
I am still doing a beginners tutorial in Rails so the above is the only thing I could think of to try.
我现在尝试了 Shivam 的建议,但现在得到以下内容:
I have now tried Shivam's suggestion but now get the following:
== 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$
推荐答案
确保迁移文件与运行迁移期间的完全相同,然后像这样回滚迁移:
Make sure the migration file is exactly the same as it was during running the migration and then rollback the migration like this:
rake db:rollback STEP=1 # rollbacks 1 step
完成后,相应地编辑文件并:
Once that is done, edit the file accordingly and:
rake db:migrate
或者您可以通过以下方式具体说明版本:
or you can specifically tell the version by:
rake db:migrate VERSION=20141222070950 #verison is timestamp from your migration filename
或者
我可以看到您想要做的就是在模型表中添加一个新列 subject_id
.您也可以通过运行独立迁移来实现此目的:>
I can see all you want to do is add a new column subject_id
to you model table. You can achieve this by running a standalone migration too:
rails generate migration AddSubjectIdToPages subject_id:integer
紧随其后:
rake db:migrate
这篇关于Ruby on Rails Mysql2::Error: Table 'pages' 已经存在:CREATE TABLE `pages` 但不能迁移回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!