2

我在旧数据库中有一个表,其中有一个名为字段的列。将表直接链接到 activeRecord 模型是不可能的,因为后者会尝试创建方法 field_changed? 这已经定义了。

所以我创建了一个视图来重命名字段,并创建了以下规则来允许插入、更新和删除。

create or replace rule new_linked_fields_upd as on update to new_linked_fields 
     do instead 
     update linked_fields set race_id = new.race_id , master_id = new.master_id, field = new.field_name
     where id = new.id;

create or replace rule new_linked_fields_ins as on insert to new_linked_fields
     do instead
     insert into linked_fields select nextval('linked_fields_id_seq'), new.race_id, new.field_name, new.master_id;

create or replace rule new_linked_fields_del as on delete to new_linked_fields
     do instead
      delete from linked_fields where id = old.id;

我将 activeRecord 模型链接到视图。

class LinkedField < ActiveRecord::Base
  self.table_name ="new_linked_fields"
  self.primary_key = "id"
end

我必须添加 self.primary_key = "id",否则 LinkedField.last 等方法会失败(生成错误的 SQL)。

在 SQL 中,一切都很好。在 ruby​​ 中,我可以更新 LinkedField 的属性,或者销毁一个实例,但我不能创建一个。

LinkedField.create(field_name:'toto',race_id:-3,master_id:4)

返回以下错误。

   (0.3ms)  BEGIN
  SQL (1.0ms)  INSERT INTO "new_linked_fields" ("field_name", "master_id", "race_id") VALUES ($1, $2, $3) RETURNING "id"  [["field_name", "toto"], ["master_id", 4], ["race_id", -3]]
   (0.3ms)  ROLLBACK
ActiveRecord::StatementInvalid: PG::Error: ERROR:  syntax error at end of input
LINE 1: DEALLOCATE
                   ^
: INSERT INTO "new_linked_fields" ("field_name", "master_id", "race_id") VALUES ($1, $2, $3) RETURNING "id"
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:297:in `exec'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:297:in `dealloc'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:287:in `delete'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:1172:in `rescue in exec_cache'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:1155:in `exec_cache'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:665:in `block in exec_query'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:663:in `exec_query'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:63:in `exec_insert'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/relation.rb:66:in `insert'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/persistence.rb:363:in `create'
     from /Users/macbook/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/timestamp.rb:57:in `create'

有什么提示吗?我正在使用 postgres 9.0 和 rails 3.2.6。谢谢

4

1 回答 1

4

首先,如果您能够更新到 PostgreSQL 9.1,那么视图触发器比规则更容易正确。

第二,从哪里来DEALLOCATE?它是否试图释放准备好的声明?

尝试启用log_statement = 'all'in postgresql.conf,重新加载 PostgreSQL(pg_ctl reloadSIGHUPpostmaster,或重新启动它),重新运行测试并检查 Pg 的日志。查看 Rails 程序实际运行的 SQL。

于 2012-08-13T13:49:32.093 回答