【问题标题】:Converting a serialized array to a PSQL array, error将序列化数组转换为 PSQL 数组,错误
【发布时间】:2014-03-18 20:07:47
【问题描述】:

所以我正在尝试进行迁移,该迁移采用现有列,这是一个序列化数组(文本),并将其转换为 psql 数组。我尝试了很多东西,但总是出错。

这就是我的迁移的样子:

 class RenameColumn < ActiveRecord::Migration
    def up
      rename_column :table_things, :column, :old_column

      add_column :table_things, :column, :text, :array => true, null: false, :default => []

      TableThing.class_eval do
        serialize :old_column, Array
      end

      TableThing.reset_column_information

      TableThing.find_each{|tt|
        tt.column = tt.old_column
        tt.save!
      }
     TableThing.reset_column_information
       remove_column :table_things, :old_column
    end

这是堆栈跟踪:

rake aborted!
An error has occurred, this and all later migrations canceled:    ActiveRecord::RecordNotSaved/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/persistence.rb:125:in `save!'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/validations.rb:57:in `save!'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:275:in `block in save!'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:330:in `block in with_transaction_returning_status'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:209:in `transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:327:in `with_transaction_returning_status'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:275:in `save!'
    /Users/username/code/work/Jobber/db/migrate/20140317215526_rename_addons.rb:15:in `block in up'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:26:in `block (2 levels) in find_each'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:26:in `each'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:26:in `block in find_each'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:76:in `find_in_batches'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/relation.rb:70:in `find_in_batches'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:25:in `find_each'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/querying.rb:8:in `find_each'
    /Users/username/code/work/Jobber/db/migrate/20140317215526_rename_addons.rb:13:in `up'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:578:in `exec_migration'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:559:in `block (2 levels) in migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:558:in `block in migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:557:in `migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:713:in `migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:963:in `block in execute_migration_in_transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:1009:in `block in ddl_transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:221:in `within_new_transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:209:in `transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:1009:in `ddl_transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:962:in `execute_migration_in_transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:924:in `block in migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:920:in `each'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:920:in `migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:768:in `up'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:746:in `migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
    /Users/username/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
    /Users/username/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'

有人知道我做错了什么,或者我可以尝试解决我的问题吗?

【问题讨论】:

  • 请提供带有堆栈跟踪的错误文本。
  • 抱歉,请稍等
  • 我认为错误消息不会有太大帮助,而且它发生在 add_column 之后
  • 您的模型上是否有一些 before_save 回调可能会干扰?

标签: database postgresql ruby-on-rails-4


【解决方案1】:

从 .save 更改! to .save 有助于显示更好的错误消息,最终问题在于回调,因此从

  TableThing.find_each{|tt|
    tt.column = tt.old_column
    tt.save!
  }

  TableThing.find_each{|tt|
    tt.update_column(:column, tt.old_column)
  }

解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    相关资源
    最近更新 更多