PG::UndefinedTable: ERROR: relation "step_images" does not exist
PG::UndefinedTable: ERROR: relation "step_images" does not exist
在迁移时我收到以下错误消息:
PG::UndefinedTable: ERROR: relation "step_images" does not exist LINE 5: WHERE a.attrelid = '"step_images"'::regclass ^ : SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '"step_images"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
分贝
class CreateStepImages < ActiveRecord::Migration
def change
create_table :step_images do |t|
t.string :text
t.attachment :image1
t.attachment :image2
t.attachment :image3
t.attachment :image4
t.attachment :image5
t.references :steptation, index: true
t.timestamps
end
end
end
在 step_image 模型中
class StepImage < ActiveRecord::Base
belongs_to :steptation
belongs_to :user
end
步进模式
has_many :step_images
在 step_images 控制器中
class StepImagesController < ApplicationController
before_action :find_step_image ,only:[:show,:update,:edit,:destroy]
before_action :set_steptation, only: [:new, :create]
def new
@steptation =Steptation.find(params[:id])
@step_image = @steptation.step_images.build
@user = current_user
end
def index
end
def create
@step_image = Step_image.new(step_image_params)
@step_image.user_id = current_user.id
if @step_image.save
@steptation.step_images << @step_image
redirect_to case_path(id: @steptation.case_id)
else
render :new
end
end
def edit
end
def update
if @step_image.update(step_image_params)
redirect_to root_path, alert: "steptation Information updated successfully"
else
flash.now[:error] = "Couldn't update!"
render :edit
end
end
def destroy
@step_image.destroy
redirect_to root_path, notice: "step_image deleted Successfully"
end
private
def set_steptation
@steptation = Steptation.find(params[:id])
end
def find_step_image
@step_image = Step_image.find(params[:id])
end
def step_image_params
params.require(:step_image).permit(:text,:image1,:image2,:image3,:image4,:image5)
end
end
我做到了rake db:rollback STEP=1
$ rake db:rollback STEP=1
ActiveRecord::SchemaMigration Load (0.9ms) SELECT "schema_migrations".* FROM "schema_migrations"
ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to CreateStepImages (20201119214001)
(0.1ms) BEGIN
== 20201119214001 CreateStepImages: reverting =================================
-- drop_table(:step_images)
(10.3ms) DROP TABLE "step_images"
(0.2ms) ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: table "step_images" does not exist
: DROP TABLE "step_images"
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activesupport-4.2.11.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:127:in `drop_table'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:665:in `block in method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:499:in `block in revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:606:in `exec_migration'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:590:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:768:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1023:in `block in execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `block in ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/transactions.rb:220:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1022:in `execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:984:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:830:in `down'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:935:in `move'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:812:in `rollback'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:118:in `block (2 levels) in <top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `eval'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: table "step_images" does not exist
: DROP TABLE "step_images"
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activesupport-4.2.11.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:127:in `drop_table'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:665:in `block in method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:499:in `block in revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:606:in `exec_migration'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:590:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:768:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1023:in `block in execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `block in ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/transactions.rb:220:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1022:in `execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:984:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:830:in `down'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:935:in `move'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:812:in `rollback'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:118:in `block (2 levels) in <top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `eval'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `<main>'
Caused by:
PG::UndefinedTable: ERROR: table "step_images" does not exist
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:127:in `drop_table'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:665:in `block in method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:499:in `block in revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:606:in `exec_migration'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:590:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:768:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1023:in `block in execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `block in ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/transactions.rb:220:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1022:in `execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:984:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:830:in `down'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:935:in `move'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:812:in `rollback'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:118:in `block (2 levels) in <top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `eval'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => db:rollback
(See full trace by running task with --trace)
当 运行ning rake db:migrate
时,rails 将在执行迁移之前加载完整的应用程序代码。因此,当你已经定义了StepImage
class时,rails会尝试寻找对应的table。但是,此时 table 尚未创建,从而导致此错误。尝试注释掉 StepImage
class 和与之相关的所有内容(例如,通过将文件移出项目文件夹)并重新 运行 迁移。如果有效,请将文件移回原处。
似乎回滚失败的原因相同:从未创建 step_images table。在这种情况下,我通常会执行以下操作:
将迁移内容放入begin/rescue/end:
默认更改
开始
create_table :step_images 做 |t|
t.string:文本
t.attachment:image1
#...
结束
救援
结束
结束
回滚此迁移以返回原始状态。救援块将捕获错误并且迁移应该成功。
使用 psql 检查 step_images table 是否真的消失了。如果没有,用psql删除。
清理源代码:删除所有与新 step_images table 有关的内容。从这次迁移开始。将其移出项目,这样 rails c
就不会抱怨挂起的迁移。然后将所有模型、控制器等移出项目。删除所有指向新模型的关联(注释掉)。否则,指向不存在的关系 models/tables 将导致更多错误。
清理直到 rails c
成功启动应用程序。
将迁移移回 db/migrations 和 运行。
如果成功,请将所有内容重新带回。如果不确定,请分多个小步骤进行,并在中间通过 运行ning rails c
.
检查
希望对您有所帮助!
在迁移时我收到以下错误消息:
PG::UndefinedTable: ERROR: relation "step_images" does not exist LINE 5: WHERE a.attrelid = '"step_images"'::regclass ^ : SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '"step_images"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
分贝 class CreateStepImages < ActiveRecord::Migration
def change
create_table :step_images do |t|
t.string :text
t.attachment :image1
t.attachment :image2
t.attachment :image3
t.attachment :image4
t.attachment :image5
t.references :steptation, index: true
t.timestamps
end
end
end
在 step_image 模型中
class StepImage < ActiveRecord::Base
belongs_to :steptation
belongs_to :user
end
步进模式
has_many :step_images
在 step_images 控制器中
class StepImagesController < ApplicationController
before_action :find_step_image ,only:[:show,:update,:edit,:destroy]
before_action :set_steptation, only: [:new, :create]
def new
@steptation =Steptation.find(params[:id])
@step_image = @steptation.step_images.build
@user = current_user
end
def index
end
def create
@step_image = Step_image.new(step_image_params)
@step_image.user_id = current_user.id
if @step_image.save
@steptation.step_images << @step_image
redirect_to case_path(id: @steptation.case_id)
else
render :new
end
end
def edit
end
def update
if @step_image.update(step_image_params)
redirect_to root_path, alert: "steptation Information updated successfully"
else
flash.now[:error] = "Couldn't update!"
render :edit
end
end
def destroy
@step_image.destroy
redirect_to root_path, notice: "step_image deleted Successfully"
end
private
def set_steptation
@steptation = Steptation.find(params[:id])
end
def find_step_image
@step_image = Step_image.find(params[:id])
end
def step_image_params
params.require(:step_image).permit(:text,:image1,:image2,:image3,:image4,:image5)
end
end
我做到了rake db:rollback STEP=1
$ rake db:rollback STEP=1
ActiveRecord::SchemaMigration Load (0.9ms) SELECT "schema_migrations".* FROM "schema_migrations"
ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to CreateStepImages (20201119214001)
(0.1ms) BEGIN
== 20201119214001 CreateStepImages: reverting =================================
-- drop_table(:step_images)
(10.3ms) DROP TABLE "step_images"
(0.2ms) ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: table "step_images" does not exist
: DROP TABLE "step_images"
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activesupport-4.2.11.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:127:in `drop_table'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:665:in `block in method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:499:in `block in revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:606:in `exec_migration'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:590:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:768:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1023:in `block in execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `block in ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/transactions.rb:220:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1022:in `execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:984:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:830:in `down'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:935:in `move'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:812:in `rollback'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:118:in `block (2 levels) in <top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `eval'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: table "step_images" does not exist
: DROP TABLE "step_images"
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activesupport-4.2.11.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:127:in `drop_table'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:665:in `block in method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:499:in `block in revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:606:in `exec_migration'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:590:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:768:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1023:in `block in execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `block in ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/transactions.rb:220:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1022:in `execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:984:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:830:in `down'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:935:in `move'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:812:in `rollback'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:118:in `block (2 levels) in <top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `eval'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `<main>'
Caused by:
PG::UndefinedTable: ERROR: table "step_images" does not exist
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:484:in `block in log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:478:in `log'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:127:in `drop_table'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:665:in `block in method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:499:in `block in revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:498:in `revert'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:606:in `exec_migration'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:590:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:768:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1023:in `block in execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `block in ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/transactions.rb:220:in `transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1069:in `ddl_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:1022:in `execute_migration_in_transaction'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:984:in `block in migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `each'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:980:in `migrate'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:830:in `down'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:935:in `move'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/migration.rb:812:in `rollback'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:118:in `block (2 levels) in <top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `eval'
/Users/nour/.rvm/gems/ruby-2.5.5/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => db:rollback
(See full trace by running task with --trace)
当 运行ning rake db:migrate
时,rails 将在执行迁移之前加载完整的应用程序代码。因此,当你已经定义了StepImage
class时,rails会尝试寻找对应的table。但是,此时 table 尚未创建,从而导致此错误。尝试注释掉 StepImage
class 和与之相关的所有内容(例如,通过将文件移出项目文件夹)并重新 运行 迁移。如果有效,请将文件移回原处。
似乎回滚失败的原因相同:从未创建 step_images table。在这种情况下,我通常会执行以下操作:
将迁移内容放入begin/rescue/end:
默认更改
开始
create_table :step_images 做 |t|
t.string:文本
t.attachment:image1
#...
结束
救援
结束
结束回滚此迁移以返回原始状态。救援块将捕获错误并且迁移应该成功。
使用 psql 检查 step_images table 是否真的消失了。如果没有,用psql删除。
清理源代码:删除所有与新 step_images table 有关的内容。从这次迁移开始。将其移出项目,这样
rails c
就不会抱怨挂起的迁移。然后将所有模型、控制器等移出项目。删除所有指向新模型的关联(注释掉)。否则,指向不存在的关系 models/tables 将导致更多错误。清理直到
rails c
成功启动应用程序。将迁移移回 db/migrations 和 运行。
如果成功,请将所有内容重新带回。如果不确定,请分多个小步骤进行,并在中间通过 运行ning
检查rails c
.
希望对您有所帮助!