简体   繁体   English

在rails3迁移中,“PGError:错误:当前事务被中止”

[英]“PGError: ERROR: current transaction is aborted” in rails3 migration

I'm under Rails 3.0.9, with Ruby 1.9.2 (p290). 我在Rails 3.0.9下,使用Ruby 1.9.2(p290)。 Using Postgresql 9.0.4, and the 'pg' gem v0.11.0 使用Postgresql 9.0.4和'pg'gem v0.11.0

The problem is : 问题是 :

I've got a really simple migration just changing the value of a column with conditions : 我有一个非常简单的迁移只是改变条件的值有条件:

def self.up
  Closet.reset_column_information
  say_with_time "Unifying gender column to h/f" do
    Closet.connection.update "UPDATE closets AS c SET gender='h' WHERE c.gender IN ('homme', 'Homme', 'men', 'Men');"
    Closet.connection.update "UPDATE closets AS c SET gender='f' WHERE c.gender IN ('femme', 'Femme', 'women', 'Women');"
  end
end

Every request works perfectly in my erb console and in the pgAdmin SQL's console too, but when I run the migration it says : 每个请求在我的erb控制台和pgAdmin SQL的控制台中也能完美运行,但是当我运行迁移时它会说:

PGError: ERROR:  current transaction is aborted, commands ignored until end of transaction block

If anyone as an idea... 如果有人作为一个想法......

Here is bigger part of the error message stack : 这是错误消息堆栈的更大部分:

== MigrateClosets: migrating ================================================= rake aborted! == MigrateClosets:迁移============================================= ====耙子中止了! An error has occurred, this and all later migrations canceled: 发生错误,此以及所有后续迁移都已取消:

PGError: ERROR:  current transaction is aborted, commands ignored until end of transaction block
: UPDATE closets SET gender='h' WHERE closets.gender  = 'homme';
/Users/gdurelle/.rvm/gems/ruby-1.9.2-p290@dressmeNG/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `rescue in log'
/Users/gdurelle/.rvm/gems/ruby-1.9.2-p290@dressmeNG/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log'
/Users/gdurelle/.rvm/gems/ruby-1.9.2-p290@dressmeNG/gems/activerecord-3.0.9/lib/active_record/connection_adapters/postgresql_adapter.rb:514:in `execute'
/Users/gdurelle/.rvm/gems/ruby-1.9.2-p290@dressmeNG/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:288:in `update_sql'
/Users/gdurelle/.rvm/gems/ruby-1.9.2-p290@dressmeNG/gems/activerecord-3.0.9/lib/active_record/connection_adapters/postgresql_adapter.rb:525:in `update_sql'
/Users/gdurelle/.rvm/gems/ruby-1.9.2-p290@dressmeNG/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:49:in `update'
/Users/gdurelle/.rvm/gems/ruby-1.9.2-p290@dressmeNG/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/query_cache.rb:16:in `update'
/Users/gdurelle/Sites/rails/DressMeNextGen/db/migrate/20110613125139_migrate_closets.rb:4:in `up'
/Users/gdurelle/.rvm/gems/ruby-1.9.2-p290@dressmeNG/gems/activerecord-3.0.9/lib/active_record/migration.rb:314:in `block in migrate'
/Users/gdurelle/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:295:in `measure'
/Users/gdurelle/.rvm/gems/ruby-1.9.2-p290@dressmeNG/gems/activerecord-3.0.9/lib/active_record/migration.rb:314:in `migrate'

Here are a couple wild guesses that might help. 以下是一些可能有所帮助的疯狂猜测。 Looks like you have a transaction aborting, being caught, and ignored somewhere; 看起来你有一个交易中止,被抓住,并在某处被忽略; that can mess up the entire transaction the self.up runs in and that would explain your error message and the behavior you're seeing. 这会搞乱self.up运行的整个事务,这可以解释你的错误信息和你所看到的行为。

The reset_column_information call usually goes after the database change and only if the schema has changed and you need to use the new schema for the rest of the migration; reset_column_information调用通常数据库更改之后进行 ,并且仅在架构已更改且您需要使用新架构进行其余迁移时; neither of these apply to you so you can drop Closet.reset_column_information completely. 这些都不适用于您,因此您可以完全删除Closet.reset_column_information

You should also have an execute method available in your migrations so there's no need to talk to the Closet at all. 您还应该在迁移中使用execute方法,因此根本不需要与Closet通信。 Also, you don't need semicolons at the end of your SQL statements; 此外,在SQL语句的末尾不需要分号; they probably won't hurt but if we strip this right down to the bare essentials we might make the problem go away. 他们可能不会受到伤害,但如果我们将这一点剥离到最基本的要素,我们可能会让问题消失。

Try this and see what happens: 试试这个,看看会发生什么:

def self.up
  say_with_time "Unifying gender column to h/f" do
    execute %q{UPDATE closets SET gender = 'h' WHERE gender IN ('homme', 'Homme', 'men', 'Men')}
    execute %q{UPDATE closets SET gender = 'f' WHERE gender IN ('femme', 'Femme', 'women', 'Women')}
  end
end

This gives us the bare minimum that we need to get your database updated. 这为我们提供了更新数据库所需的最低限度。 Hopefully the stray transaction problem went away with all the other stuff you didn't need. 希望流浪交易问题与你不需要的所有其他东西一起消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM