【问题标题】:Problems running migrations against users table with Postgresql使用 Postgresql 对用户表运行迁移时出现问题
【发布时间】:2011-08-19 19:47:59
【问题描述】:

我正在尝试为迁移中设置的新列更新一些默认值。但是,每当我尝试对用户表的记录执行任何操作(修改其结构除外)时,都会出现 Postgres 错误。我正在使用 Rails 3.0.7、ruby 1.9.2 和 pg gem 版本 0.11.0

这里是迁移:

def self.up
 add_column :users, :state_machine, :string
 add_column :users, :wizard_steps_completed, :integer, :default => "1"
 add_column :users, :activated_at, :datetime

 User.reset_column_information
 User.all.each do |u|
   u.update_attributes(:state_machine => "activated", :wizard_steps_completed => 3, :activated_at => u.created_at)
 end
end

添加列没有问题。但是对现有记录的更改都失败并出现以下错误:

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
==  AddUserSignupInfo: migrating ==============================================
rake aborted!
An error has occurred, this and all later migrations canceled:

PGError: ERROR:  current transaction is aborted, commands ignored until end of transaction block
:             SELECT COUNT(*)
            FROM pg_tables
            WHERE tablename = 'users'

如果我尝试更新任何似乎有效的 orecord,我只能进行结构更改...

有什么想法吗?

【问题讨论】:

  • 你的 User 类是否对你的新属性做了一些有趣的事情?
  • 没有。只是香草。为了简化,我将更新移动到他们自己的迁移中并更新为使用 update_all 只是为了使其理论上成为一个事务。它在控制台中工作。
  • 我猜某处某处触发了数据库中的错误,然后捕获并纠正它(例如 INSERT,违反唯一约束,捕获异常,改为执行更新),但这是破坏db:migrate 交易。如果分两步完成,那么我会继续做更有趣的事情。

标签: ruby-on-rails ruby postgresql migration


【解决方案1】:

打开 postgres 日志记录(在 /var/lib/pgsql/data/postgresql.conf 和 grep 中配置为“错误报告和日志记录”)。或者您可能想要获取 SQL 并自己运行它以查看发生了什么错误。这可能是由于您的更新而失败的约束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-12
    • 2023-04-10
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 2023-03-15
    相关资源
    最近更新 更多