Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rake测试不使用序列复制开发postgres db_Ruby On Rails_Postgresql_Activerecord_Templates_Sequence - Fatal编程技术网

Ruby on rails rake测试不使用序列复制开发postgres db

Ruby on rails rake测试不使用序列复制开发postgres db,ruby-on-rails,postgresql,activerecord,templates,sequence,Ruby On Rails,Postgresql,Activerecord,Templates,Sequence,我试图在postgresql上开发一个rails应用程序,使用一个序列来增加字段,而不是基于validates\u university\u的默认ruby方法 事实证明,这具有挑战性,原因有很多: 1.这是现有表的迁移,而不是新表或列的迁移 2.使用参数:default=>“nextval('seq')”无效,因为它试图将其设置在括号中 3.最终通过两个步骤实现了迁移: change_column :work_commencement_orders, :wco_number_suffix, :i

我试图在postgresql上开发一个rails应用程序,使用一个序列来增加字段,而不是基于validates\u university\u的默认ruby方法

事实证明,这具有挑战性,原因有很多: 1.这是现有表的迁移,而不是新表或列的迁移 2.使用参数:default=>“nextval('seq')”无效,因为它试图将其设置在括号中 3.最终通过两个步骤实现了迁移:

change_column :work_commencement_orders, :wco_number_suffix, :integer, :null => false#, :options => "set default nextval('wco_number_suffix_seq')"

execute %{
  ALTER TABLE work_commencement_orders ALTER COLUMN wco_number_suffix SET DEFAULT nextval('wco_number_suffix_seq');
}
现在,这在开发数据库中似乎做了正确的事情,模式如下所示:

wco_number_suffix      | integer                     | not null default nextval('wco_number_suffix_seq'::regclass)
然而,这些测试都以失败告终

PGError: ERROR:  null value in column "wco_number_suffix" violates not-null constraint
: INSERT INTO "work_commencement_orders" ("expense_account_id", "created_at", 
"process_id", "vo2_issued_on", "wco_template", "updated_at", "notes", "process_type", 
"vo_number", "vo_issued_on", "vo2_number", "wco_type_id", "created_by", 
"contractor_id", "old_wco_type", "master_wco_number", "deadline", "updated_by", 
"detail", "elective_id", "authorization_batch_id", "delivery_lat", "delivery_long", 
"operational", "state", "issued_on", "delivery_detail") VALUES(226, '2010-05-31 
07:02:16.764215', 728, NULL, E'Default', '2010-05-31 07:02:16.764215', NULL, 
E'Procurement::Process', NULL, NULL, NULL, 226, NULL, 276, NULL, E'MWCO-213', 
'2010-06-14 07:02:16.756952', NULL, E'Name 4597', 220, NULL, NULL, NULL, 'f', 
E'pending', NULL, E'728 Test Road; Test Town; 1234; Test Land') RETURNING "id"
检查测试数据库的架构时可以找到解释:

wco_number_suffix      | integer                     | not null
那么违约发生了什么

我试着加上

task:
    template: smmt_ops_development
到database.yml文件,该文件具有发出

create database smmt_ops_test template = "smmt_ops_development" encoding = 'utf8'
我已经验证过,如果我发布这个,那么它实际上复制了默认的nextval。很明显,rails在这之后又在做一些事情来抑制它

关于如何解决这个问题有什么建议吗

谢谢
Robert

我并没有解决这个问题,但为work_Start_orders模型开发了以下变通方法,以使测试能够运行

###########################################################################
###########################################################################
# NOTE this is purely for test, it should have no effect on development or production
# as the field will be marked readonly and prevented from being written to the db so that
# it picks up a default value from its sequence.
public

def before_validation
  if wco_number_suffix.blank?
    maximum = WorkCommencementOrder::Entity.maximum(:wco_number_suffix)
    maximum ||= 0
    self.wco_number_suffix = maximum + 1
  end
end

private

def test?
  @is_test ||= (ENV['RAILS_ENV'] == 'test')
end

# Override default behaviour so that when not in test environment it does not try
# to write readonly attributes during create but instead allows them to be initialised
# by default value.
def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = test?, attribute_names = @attributes.keys)
  super(include_primary_key, include_readonly_attributes, attribute_names)
end

###########################################################################
###########################################################################

我偶然发现了同样的问题。未来的访客

基本上,将其放在config/environments/development.rb中并运行测试

    config.active_record.schema_format = :sql
更多信息-