【发布时间】:2017-01-10 08:22:43
【问题描述】:
我是 Rails 新手,我想在迁移文件中添加一个字符串。不幸的是,我不知道玩迁移是非常危险的。
ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
我相信我使用了rake db:rollback,我使用了rake db:redo,但没有任何变化我一直收到同样的错误
当我尝试rake db:migrate:status 时,我得到了这样的结果:
database: /home/ubuntu/workspace/db/development.sqlite3
Status Migration ID Migration Name
--------------------------------------------------
up 20161022035511 Create posts
up 20161022044605 Devise create users
up 20161022045410 Add user id to post
up 20161022050429 Add name to user
up 20161022054826 Add attachment image to posts
up 20161022170851 Create comments
down 20161022184713 Acts as votable migration
down 20161223064636 Add cached votes to posts
这是AddCachedVotesToPosts的内容:
class AddCachedVotesToPosts < ActiveRecord::Migration
def self.up
add_column :posts, :cached_votes_total, :integer, :default => 0
add_column :posts, :cached_votes_score, :integer, :default => 0
add_column :posts, :cached_votes_up, :integer, :default => 0
add_column :posts, :cached_votes_down, :integer, :default => 0
add_column :posts, :cached_weighted_score, :integer, :default => 0
add_column :posts, :cached_weighted_total, :integer, :default => 0
add_column :posts, :cached_weighted_average, :float, :default => 0.0
add_index :posts, :cached_votes_total
add_index :posts, :cached_votes_score
add_index :posts, :cached_votes_up
add_index :posts, :cached_votes_down
add_index :posts, :cached_weighted_score
add_index :posts, :cached_weighted_total
add_index :posts, :cached_weighted_average
# Uncomment this line to force caching of existing votes
# Post.find_each(&:update_cached_votes)
end
def self.down
remove_column :posts, :cached_votes_total
remove_column :posts, :cached_votes_score
remove_column :posts, :cached_votes_up
remove_column :posts, :cached_votes_down
remove_column :posts, :cached_weighted_score
remove_column :posts, :cached_weighted_total
remove_column :posts, :cached_weighted_average
end
end
这是我在 rails c 中得到的: 请帮帮我!
【问题讨论】:
-
运行
bundle exec rake db:migrate:up VERSION=20161223064636会得到什么? -
我假设您的
rake db:migrate:status仍然显示 down 用于迁移VERSION=20161223064636。根据错误,您确实已将列cached_votes_total添加到您的posts表中。 -
你是对的
-
如果您的迁移
VERSION=20161223064636仍然 down ,我建议您删除该迁移文件。相反,根据需要向change_column写入新的迁移或创建新的迁移文件以删除现有列并再次添加它们!
标签: ruby-on-rails migration rake