Hol*_*bæk 31 ruby database ruby-on-rails
我用devise和漂亮的生成器创建了一个数据库.我正在尝试使用nifty generator(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)
Run Code Online (Sandbox Code Playgroud)
我正在学习教程,并且很难理解为什么会这样.谁能解释一下发生了什么?
Art*_*huk 65
在create_users迁移(APP_ROOT/db/migrate/..)中,在drop_table :users之前添加create_table :users并运行rake db:migrate.它将在重新创建之前删除users表.您可以在运行此迁移后删除该行代码,以便以后不会出现错误.如果您没有对数据库的UI访问权限(例如,在heroku上),这只是一个小修复.
Gon*_*tti 22
您需要从sql lite控制台中删除该表(您将丢失其中包含的所有数据)
访问sql lite控制台,输入终端
mysql <DB NAME HERE>
删除表(不要忘记最后一个;(分号))
drop table table_name;
再次运行db:migrate
bin/rake db:migrate
希望它有所帮助,它对我有用
Sur*_*rya 16
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
Run Code Online (Sandbox Code Playgroud)
Pau*_*reu 12
迁移正在尝试创建数据库中已存在的表.
尝试从数据库中删除用户表.您的迁移过程出了问题.您还应该将schema.rb版本与db/migrate/*.rb文件进行比较.
澄清:
似乎许多SO用户不同意我的回复,因为他们认为它不准确或不推荐.
删除表总是具有破坏性,我认为每个人都理解这一点.
我应该提到add_column,因为该表是在另一个迁移文件中创建的.
And*_*ase 11
如果您知道数据库已正确创建,则只需注释掉迁移代码的创建部分即可.例如:
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
Run Code Online (Sandbox Code Playgroud)
如果表已创建,但后来的命令由于某种原因未完成,您可以保留后面的选项,例如:
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
Run Code Online (Sandbox Code Playgroud)
如果您的数据库中没有任何重要数据要保留,那么您可以将其删除表和所有数据并重新创建.例如(请注意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
Run Code Online (Sandbox Code Playgroud)
不要删除表.数据>迁移!
数据库的版本已经反映了导致错误的迁移尝试添加的更改.换句话说,如果可以跳过迁移,那么一切都会好的.检查db_schema_migrations表并尝试插入错误迁移的版本(例如,20151004034808).在我的情况下,这导致后续迁移完美执行,一切似乎都很好.
仍然不确定是什么原因导致这个问题