ruby-on-rails - PG::ObjectNotInPrerequisiteState:错误:无法插入 View

标签 ruby-on-rails postgresql

我对我收到的一个 Controller 操作错误感到非常困惑。我收到的一个操作 positions#one_click_create 的错误导致我出现此错误:

ActiveRecord::StatementInvalid: PG::ObjectNotInPrerequisiteState: ERROR:  cannot insert into view "contributions" DETAIL:  Views that do not select from a single table or view are not automatically updatable.


HINT:  To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.
    : INSERT INTO "contributions" ("person_id", "project_id", "position_id", "name_fact_id") VALUES ($1, $2, $3, $4) RETURNING "id"
        test/controllers/positions_controller_test.rb:420:in `block (3 levels) in <top (required)>

错误继续提示一个 postgresql 触发器 INSTEAD OF INSERT 触发器。

我已阅读此处找到的相关链接 INSTEAD OF INSERT但我仍然不明白问题是什么,甚至不明白如何尝试解决它。我有一个简单的 get 请求,我不明白为什么它不起作用。

对于初学者,我有这段代码。

- options = { project_name: contribution.project.url_name,
          committer_name: contribution.contributor_fact.name.name,
          invite: params[:invite] }

%button.btn.btn-primary
   = link_to t('.claim'), one_click_create_account_positions_path(options.merge(account_id: 'me')), { style: 'color: white' }

代码在执行一些 before_actions 之前点击 positions#one_click_create 的 get 操作,以查找相关模型。

one_click_create 方法如下所示。

 def one_click_create
  result = current_user.position_core.ensure_position_or_alias!(@project, @name)

  if result.class == Alias
    flash[:success] = t('.one_click_commit_claim', name: @name.name, preferred_name: result.preferred_name.name)
    redirect_to acount_positions_path(current_user)
  elsif result.class == Position
    flash[:success] = t('.one_click_contribution_success', name: @name.name)
    redirect_to accounts_positions_path(current_user)
  end

  flash[:success] = t('.one_click_submit_form', name: @name.name)
  redirect_to new_account_position_path(current_user,
                                      project_name: @project.name,
                                      committer_name: @name.name,
                                      invite: params[:invite])

 end

在我的特定情况下,条件逻辑并不重要。重要的是,当它到达重定向到 new_account_position_path 的最后一行时,我收到了此 Controller 测试所示的错误。

it 'should successfully redirect to account new page with proper params' do
  position = create_position
  contribution = create(:contribution, position: position)
  get :one_click_create, account_id: 'me', params: { project_name: contribution.project.url_name,
                          committer_name: contribution.contributor_fact.name.name,
                          invite: params[:invite]
  must_redirect_to new_account_position_path(account_id: 'me')
end

有人可以帮助阐明这里的问题是什么以及为什么它“无法插入 View :贡献”吗?

最佳答案

View 是通过对其他真实表或虚拟表执行查询而获得的虚拟表。出于这个原因,可以在特殊情况下修改 View ,特别是当它是从单个(实际)表定义时,并且在这种情况下,也是在特定条件下。如果您的程序试图将某些内容插入到未在单个表上定义的 View 中,并且满足这些特殊条件,则 PostgreSQL 会报错。查看 PostgreSQL 手册的“可更新 View ”部分 http://www.postgresql.org/docs/9.4/interactive/sql-createview.html .您可以使用其他方法来修改用于定义 View 的(实际)表,如错误消息中的提示所述。

关于ruby-on-rails - PG::ObjectNotInPrerequisiteState:错误:无法插入 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31031848/

相关文章:

ruby-on-rails - Rails 3 + rails_admin + MongoDB 如何表示数组

ruby-on-rails - 验证附件内容类型回形针

postgresql - postgres jsonb,查询对象数组

postgresql - 查询以在亚马逊 Redshift 中获取从给定日期到 48 小时的数据

sql - 从两个表中获取数据,其中该表的数据不常见

ruby-on-rails - CanCanCan 用于没有模型的自定义 Controller

ruby-on-rails - 在 Ruby on Rails 2.3.2 中,如何在创建操作期间打印出参数?

ruby-on-rails - 部署时的 ActiveSupport::MessageEncryptor:assets:precompile

mysql - lastInsertId(tableName_id_seq) 返回 :Undefined table: 7 ERROR: relation "tableName_id_seq" does not exist

postgresql 客户端 - 在 php :5-apache 上安装