我正在关注Michael Hartl的书,我在运行服务器时收到此错误:
我运行服务器时收到ActiveRecord :: PendingMigrationError,显示:
迁移正在等待中 . 要解决此问题,请运行:bin / rails db:migrate RAILS_ENV = development
请我长期坚持这个错误 .
当我输入 $ RAILS_ENV=development rake db:migrate
时出现此错误:
== 20161209073230 AddActivationToUsers:迁移============================= - add_column(:users,:activation_digest,:string)rake中止! StandardError:发生错误,此以及所有后续迁移都被取消:SQLite3 :: SQLException:重复列名:activation_digest:ALTER TABLE“users”ADD“activation_digest”varchar(必需)>'任务:TOP => db:migrate(请参阅使用--trace运行任务的完整跟踪
测试/邮寄者/预览/ user_mailer_preview.rb
UserMailerPreview <ActionMailer :: Preview#预览此电子邮件地址为http:// localhost:3000 / rails / mailers / user_mailer / account_activation def account_activation user = User.first user.activation_token = User.new_token UserMailer.account_activation(user)end#Preview此电子邮件地址为http:// localhost:3000 / rails / mailers / user_mailer / password_reset def password_reset UserMailer.password_reset end end
Schema.rb:
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161123005710) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.string "activation_digest"
t.index ["email"], name: "index_users_on_email", unique: true end
end
最新的迁移是db / migrate / [timestamp] _add_activation_to_users.rb:
class AddActivationToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :activation_digest, :string
add_column :users, :activated, :boolean, default: falserao
add_column :users, :activated_at, :datetime
end
end
5 回答
应用未应用迁移的正确命令是
RAILS_ENV=development rake db:migrate
这只是意味着您有一个待迁移的迁移 . 创建新迁移时
这意味着它会更改数据库架构或数据库布局中的某些内容 . 为了提交更改,您必须运行:
这将查看您的迁移文件并将其应用于您的数据库 . 迁移完成后,您可以再次像往常一样运行服务器 .
如果您有更多问题,我建议您阅读Rails发布的迁移文档:
http://guides.rubyonrails.org/active_record_migrations.html
SQL命令读起来像普通英语 . 不知何故,您的某个迁移正在执行先前的迁移已经执行的操作,即将
activation_digest
列添加到db中的users表 . 如果查看目录db/migrate/
,您将看到所有迁移文件 . 如果你打开其中一个,你应该能够分辨出它在做什么 . 因此,请查看所有迁移文件,并找到两个都添加了activation_digest
列的迁移 . 如果两个迁移文件相同,则需要删除一个 - 但在删除迁移之前,请执行以下步骤:https://www.baserails.com/questions/i-messed-up-while-generating-my-migration-how-can-i-undo-it
另请参阅Rails指南中的
Rolling Back
部分:http://edgeguides.rubyonrails.org/active_record_migrations.html#reverting-previous-migrations
如果由于某种原因你没有两个相同的迁移文件都添加
activation_digest
列,例如其中一个迁移文件另外做了一些事情,那么你需要弄清楚教程中你做错了哪些步骤,然后回滚到你知道的最后一次迁移是正确的 . 最后,再次按照本教程中的步骤生成后续迁移 .您的users表似乎已经有一个名为activation_digest的列 . 从迁移文件中删除添加了列的
add_column
行,然后再次运行迁移 .我认为这个迁移文件应该有效:
好的答案非常简单!只需尝试使用命令将数据库迁移到version = 0:
rake db:migrate VERSION=0
然后运行
rake db:migrate