我正在关注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 aborted! StandardError:发生错误,此错误以及稍后的所有迁移 取消:
SQLite3 :: SQLException:重复的列名:activation_digest:ALTER 表"用户"添加" activation_digest" VARCHAR (需要)GT;'任务:TOP => db:migrate(通过运行任务查看完整跟踪 与--trace)
测试/邮寄者/预览/ user_mailer_preview.rb
UserMailerPreview<的ActionMailer ::预览
#预览此电子邮件地址 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)结束#预览此电子邮件地址 http://localhost:3000/rails/mailers/user_mailer/password_reset def 重设密码 UserMailer.password_reset结束
端
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
最新迁移是 分贝/迁移/ [时间戳] _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
答案 0 :(得分:1)
应用未应用迁移的正确命令是RAILS_ENV=development rake db:migrate
答案 1 :(得分:0)
这只是意味着您有一个待迁移的迁移。创建新迁移时
railas g migration MigrationName
这意味着它会更改数据库架构或数据库布局中的某些内容。为了提交更改,您必须运行:
bin/rails db:migrate
这将查看您的迁移文件并将其应用于您的数据库。迁移完成后,您可以再次像往常一样运行服务器。
rails server
如果您有更多问题,我建议您阅读Rails发布的迁移文档:
答案 2 :(得分:0)
SQLite3 :: SQLException:重复的列名:activation_digest:
(以下是实际修改数据库的SQL命令):
ALTER TABLE“users”ADD“activation_digest”
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
列,例如其中一个迁移文件另外做了一些事情,那么你需要弄清楚教程中你的哪些步骤做错了,然后回滚到你知道正确的最后一次迁移。最后,再次按照本教程中的步骤生成后续迁移。
答案 3 :(得分:0)
您的users表似乎已经有一个名为activation_digest的列。从迁移文件中删除添加了列的add_column
行,然后再次运行迁移。
我认为此迁移文件应该有效:
class AddActivationToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :activated, :boolean, default: false
add_column :users, :activated_at, :datetime
end
end
答案 4 :(得分:0)
好的答案非常简单!只需尝试使用以下命令将数据库迁移到version = 0:rake db:migrate VERSION=0
然后运行rake db:migrate