Ruby-on-rails Rake 中止...表 'users' 已经存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7874330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rake aborted... table 'users' already exists
提问by Holger Sindbaek
I have created a database with devise and the nifty generator. I'm trying to make a new database with the nifty generator (rails g nifty:scaffold Asset user_id:integer), but when I try to migrate the database (rake db:migrate), I get the following error:
我用设计和漂亮的生成器创建了一个数据库。我正在尝试使用漂亮的生成器 ( rails g nifty:scaffold Asset user_id:integer)创建一个新数据库,但是当我尝试迁移数据库 ( rake db:migrate) 时,出现以下错误:
charlotte-dator:showwwdown holgersindbaek$ rake db:migrate
== DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
rake aborted!
An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `email` varchar(255) DEFAULT '' NOT NULL, `encrypted_password` varchar(128) DEFAULT '' NOT NULL, `reset_password_token` varchar(255), `reset_password_sent_at` datetime, `remember_created_at` datetime, `sign_in_count` int(11) DEFAULT 0, `current_sign_in_at` datetime, `last_sign_in_at` datetime, `current_sign_in_ip` varchar(255), `last_sign_in_ip` varchar(255), `name` varchar(255), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I'm following a tutorial and have quite a hard time understanding why this happens. Can anyone explain what is going on?
我正在学习教程并且很难理解为什么会发生这种情况。任何人都可以解释发生了什么?
采纳答案by Paulo Abreu
The migration is trying to create a table that already exists in your database.
迁移正在尝试创建一个已存在于您的数据库中的表。
Try to remove the user table from your database. Something went wrong with you migration process. You should also compare your schema.rb version with your db/migrate/*.rb files.
尝试从数据库中删除用户表。您的迁移过程出了点问题。您还应该将 schema.rb 版本与 db/migrate/*.rb 文件进行比较。
Clarification:
澄清:
It seems that many SO users don't agree with my reply, either because they consider it inaccurate or not recommended.
似乎很多 SO 用户不同意我的回复,要么是因为他们认为它不准确,要么是不推荐。
Removing a table is always destructive, and I think that everyone understands that.
删除桌子总是具有破坏性的,我认为每个人都明白这一点。
I should have mentioned add_column, since the table was being created in another migration file.
我应该提到add_column,因为该表是在另一个迁移文件中创建的。
回答by Artem Kalinchuk
In your create_usersmigration (APP_ROOT/db/migrate/..), add drop_table :usersright before create_table :usersand run rake db:migrate. It will remove the users table before recreating it. You can remove that line of code after running this migration so it doesn't give you errors later on. Just a small fix if you dont have UI access to a database (on heroku, for example).
在您的create_users迁移 (APP_ROOT/db/migrate/..)drop_table :users之前,添加create_table :users并运行rake db:migrate. 它将在重新创建之前删除用户表。您可以在运行此迁移后删除该行代码,以便以后不会出现错误。如果您没有对数据库的 UI 访问权限(例如,在 heroku 上),这只是一个小修复。
回答by Gonza Piotti
You need to drop that table from the sql lite console (You will lost all the data contained in it)
您需要从 sql lite 控制台删除该表(您将丢失其中包含的所有数据)
Access the sql lite console, type in terminal
mysql <DB NAME HERE>Drop table (dont forget the last ;(semicolon))
drop table table_name;run db:migrateagain
bin/rake db:migrate
访问 sql lite 控制台,输入终端
mysql <DB NAME HERE>删除表(不要忘记最后一个;(分号))
drop table table_name;再次运行db:migrate
bin/rake db:migrate
Hope it helps, it worked for me
希望它有帮助,它对我有用
回答by Surya
If you wanna play safe and don't want to lose any data then you can check if the table exists in your database.
如果您想安全操作并且不想丢失任何数据,那么您可以检查该表是否存在于您的数据库中。
class DeviseCreateUsers < ActiveRecord::Migration
def up
if table_exists?(:users)
# update or modify columns of users table here accordingly.
else
# create table and dump the schema here
end
end
def down
# same approach goes here but in the reverse logic
end
end
回答by Andrew Case
If you know the database was created properly, you can just comment out the creation part of the migration code. For example:
如果您知道数据库已正确创建,则只需注释掉迁移代码的创建部分即可。例如:
Class ActsAsVotableMigration < ActiveRecord::Migration
def self.up
# create_table :votes do |t|
#
# t.references :votable, :polymorphic => true
# t.references :voter, :polymorphic => true
#
# t.boolean :vote_flag
#
# t.timestamps
# end
#
# add_index :votes, [:votable_id, :votable_type]
# add_index :votes, [:voter_id, :voter_type]
end
def self.down
drop_table :votes
end
end
If the table was created, but later commands weren't completed for some reason, you can just leave the later options for example:
如果表已创建,但由于某种原因没有完成后面的命令,您可以保留后面的选项,例如:
Class ActsAsVotableMigration < ActiveRecord::Migration
def self.up
# create_table :votes do |t|
#
# t.references :votable, :polymorphic => true
# t.references :voter, :polymorphic => true
#
# t.boolean :vote_flag
#
# t.timestamps
# end
add_index :votes, [:votable_id, :votable_type]
add_index :votes, [:voter_id, :voter_type]
end
def self.down
drop_table :votes
end
end
If you don't have any significant data in your database to preserve however you can just have it drop the table and all the data and create it fresh. For example (notice the "drop_table :votes", in the self.up):
如果您的数据库中没有任何要保留的重要数据,您可以让它删除表和所有数据并重新创建它。例如(注意 self.up 中的“drop_table :votes”):
class ActsAsVotableMigration < ActiveRecord::Migration
def self.up
drop_table :votes
create_table :votes do |t|
t.references :votable, :polymorphic => true
t.references :voter, :polymorphic => true
t.boolean :vote_flag
t.timestamps
end
add_index :votes, [:votable_id, :votable_type]
add_index :votes, [:voter_id, :voter_type]
end
def self.down
drop_table :votes
end
end
回答by Michael Fulton
Don't delete tables. Data > migrations!
不要删除表。数据 > 迁移!
The version of the database already reflects the changes the error-causing migration is trying to add. In other words, if the migration could be skipped, then everything would be fine. Check the db_schema_migrations table and try inserting the version of the erroneous migration (e.x, 20151004034808). In my case this caused subsequent migrations to execute perfectly and everything seems fine.
数据库的版本已经反映了导致错误的迁移试图添加的更改。换句话说,如果可以跳过迁移,那么一切都会好起来的。检查 db_schema_migrations 表并尝试插入错误迁移的版本(例如 20151004034808)。在我的情况下,这导致后续迁移完美执行,一切似乎都很好。
Still not sure what caused this problem.
仍然不确定是什么导致了这个问题。
回答by boulder_ruby
I think this is an issue unique or more common to mysql in rails, possible having to do with the mysql2 gem itself.
我认为这是 Rails 中 mysql 独有的或更常见的问题,可能与 mysql2 gem 本身有关。
I know this because I just switched from sqlite to mysql and just started having this problem systematically.
我知道这一点是因为我刚刚从 sqlite 切换到 mysql 并且刚刚开始系统地遇到这个问题。
In my case, I simply commented out the code that had already run and ran the migration again (which I'm not adding more detail to because it looks like the guy above me did that).
就我而言,我只是将已经运行的代码注释掉并再次运行迁移(我没有添加更多细节,因为看起来我上面的那个人这样做了)。
回答by Kevin Hough
I had a similar problem when trying to add Devise authentication to an existing Users table.
尝试将设计身份验证添加到现有用户表时,我遇到了类似的问题。
My solution: I found that I had two migrate files, both trying to create the Users table. So rather than deleting the table (probably not the best habit to form), I commented out the first (original) migrate file that created the Users table and then left the Devise migrate file as-is. Re-ran the migration and it worked fine.
我的解决方案:我发现我有两个迁移文件,都试图创建用户表。因此,我没有删除表(可能不是形成的最佳习惯),而是注释掉了创建用户表的第一个(原始)迁移文件,然后将设计迁移文件保持原样。重新运行迁移,它工作正常。
As it turns out, the Devise file wasn't causing the problem; I can see that it is "changing" the table, not "creating it", which means that even without the devise installation, a db:migrate probably would have caused the same issue (though I haven't tested this).
事实证明,Devise 文件并没有导致问题。我可以看到它正在“更改”表,而不是“创建它”,这意味着即使没有安装设计,db:migrate 也可能会导致相同的问题(尽管我没有对此进行测试)。
回答by Alex V
If you want to keep your data, rename the table, but do it in the migration to save time, then remove it once the migration has ran.
如果要保留数据,请重命名表,但在迁移中执行以节省时间,然后在迁移运行后将其删除。
Place at the top part of the upsection of the migration file.
放置在迁移文件的up部分的顶部。
rename_table :users, :users2
回答by stevec
If your app is new and you don't care about the data in your database, simply:
如果您的应用程序是新的并且您不关心数据库中的数据,只需:
rake db:reset

