Ruby on rails I';m无法使用视图在旧表中保存activeRecord对象

Ruby on rails I';m无法使用视图在旧表中保存activeRecord对象,ruby-on-rails,postgresql,activerecord,rails-postgresql,Ruby On Rails,Postgresql,Activerecord,Rails Postgresql,我在遗留数据库中有一个表,该表有一个名为field的列。 不可能将表直接链接到activeRecord模型,因为后者将尝试创建方法字段?这已经被定义了 因此,我创建了一个视图来重命名字段,并创建了以下规则来允许插入、更新和删除 create or replace rule new_linked_fields_upd as on update to new_linked_fields do instead update linked_fields set race_id =

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

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

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
返回以下错误

   (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。
首先谢谢你,如果你能更新到PostgreSQL 9.1,那么要想正确使用它要比使用规则容易得多

其次,
DEALLOCATE
从何而来?它是在试图释放一份事先准备好的声明吗


尝试在
postgresql.conf
中启用
log\u语句='all'
,重新加载postgresql(
pg\u ctl reload
SIGHUP
邮局主管,或重新启动它),重新运行测试并检查pg的日志。查看Rails程序实际运行的SQL语句。

首先,如果您能够更新到PostgreSQL 9.1,那么这些语句比规则更容易正确

其次,
DEALLOCATE
从何而来?它是在试图释放一份事先准备好的声明吗


尝试在
postgresql.conf
中启用
log\u语句='all'
,重新加载postgresql(
pg\u ctl reload
SIGHUP
邮局主管,或重新启动它),重新运行测试并检查pg的日志。查看Rails程序实际运行的SQL。

谢谢Craig。Postgresql对这个问题给出了明确的解释。错误:无法对关系“新建链接的\u字段”执行插入返回提示:您需要一个带有返回子句的无条件on INSERT DO INSTEAD规则。现在的规则是:创建或替换规则新建链接的\u字段\u INSERT as on INSERT to new \u linked \u fields DO INSTEAD INSERT IN INTERT into linked \u fields select nextval('链接的\u id \u seq'),new.race\u id、new.field\u name、new.master\u id返回链接的\u字段。*;您知道我是否还必须添加返回值以删除和更新规则吗?到目前为止,一切似乎都正常。@jlfenaux Re
返回关于插入、删除和更新规则的
子句,这是一个非常有趣的问题。我怀疑是这样,因为否则获取行数会很棘手,而且不可能使用
删除。。。返回
查询。看。如果有疑问,请通过
psql
在SQL级别对其进行测试。谢谢Craig。Postgresql对这个问题给出了明确的解释。错误:无法对关系“新建链接的\u字段”执行插入返回提示:您需要一个带有返回子句的无条件on INSERT DO INSTEAD规则。现在的规则是:创建或替换规则新建链接的\u字段\u INSERT as on INSERT to new \u linked \u fields DO INSTEAD INSERT IN INTERT into linked \u fields select nextval('链接的\u id \u seq'),new.race\u id、new.field\u name、new.master\u id返回链接的\u字段。*;您知道我是否还必须添加返回值以删除和更新规则吗?到目前为止,一切似乎都正常。@jlfenaux Re
返回关于插入、删除和更新规则的
子句,这是一个非常有趣的问题。我怀疑是这样,因为否则获取行数会很棘手,而且不可能使用
删除。。。返回
查询。看。如果有疑问,请通过
psql
在SQL级别对其进行测试。
   (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'