gpt4 book ai didi

database - 将序列化数组转换为 PSQL 数组,错误

转载 作者:搜寻专家 更新时间:2023-10-30 20:40:27 25 4
gpt4 key购买 nike

所以我正在尝试进行迁移,该迁移采用现有列(序列化数组(文本))并将其转换为 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>'

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

最佳答案

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

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

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

解决了问题。

关于database - 将序列化数组转换为 PSQL 数组,错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22489921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com