我在我的 4.0.0 Ruby on Rails 应用程序中的模型文件中引入了一个新字段,并使用以下行添加了验证以确保它是唯一的:
validates_uniqueness_of :source_id
紧接着,我的 RSpec/Capybara 测试套件开始出现以下错误消息:
Failure/Error: let(:product) { FactoryGirl.create(:product) }
ActiveRecord::StatementInvalid:
PG::Error: ERROR: bind message supplies 1 parameters, but prepared statement "a4" requires 0
SELECT 1 AS one FROM "products" WHERE "products"."source_id" = 'Product GUID6' LIMIT 1
我知道这与 Postgresql 准备好的语句有关,但不确定需要做些什么来解决这个问题。
作为附加信息,我已经使用 unique: true 选项更新了带有字段“source_id”索引的数据库表。
非常感谢您的意见。
我正在使用 FactoryGirl gem 来处理我的模型对象的创建,例如:
let(:product) { FactoryGirl.create(:product) }
subject { product_dimension }
describe "method" do
it { should respond_to(:product) }
end
这是来自实际 Factory.rb 文件的片段:
factory :product do
sequence(:source_id) { |n| "Product GUID#{n}" }
sequence(:title) { |n| "Prodcut #{n}" }
content "Lorem ipsum"
channel_id 1
end
这是异常堆栈跟踪:
1) ProductDimension method dimension
Failure/Error: let(:product) { FactoryGirl.create(:product) }
ActiveRecord::StatementInvalid:
PG::Error: ERROR: bind message supplies 1 parameters, but prepared statement "a4" requires 0
: SELECT 1 AS one FROM "products" WHERE "products"."source_id" = 'Product GUID6' LIMIT 1
# ./spec/models/product_dimension_spec.rb:4:in `block (2 levels) in <top (required)>'
# ./spec/models/product_dimension_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/product_dimension_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/product_dimension_spec.rb:28:in `block (3 levels) in <top (required)>'
这是更详细的堆栈跟踪:
1) ProductDimension methods dimension
Failure/Error: let(:product) { FactoryGirl.create(:product) }
ActiveRecord::StatementInvalid:
PG::Error: ERROR: bind message supplies 1 parameters, but prepared statement "a4" requires 0
: SELECT 1 AS one FROM "products" WHERE "products"."source_id" = 'GUID 6' LIMIT 1
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:780:in `get_last_result'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:780:in `exec_cache'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:139:in `block in exec_query'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:885:in `select'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:30:in `select_one'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:36:in `select_value'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/relation/finder_methods.rb:174:in `exists?'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/validations/uniqueness.rb:28:in `validate_each'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:153:in `block in validate'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:150:in `each'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:150:in `validate'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:283:in `_callback_before_17'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:467:in `_run__2082443422329982594__validate__callbacks'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations.rb:373:in `run_validations!'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations/callbacks.rb:106:in `block in run_validations!'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__2082443422329982594__validation__callbacks'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations/callbacks.rb:106:in `run_validations!'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations.rb:314:in `valid?'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/validations.rb:70:in `valid?'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/validations.rb:77:in `perform_validations'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/validations.rb:57:in `save!'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/transactions.rb:275:in `block in save!'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/transactions.rb:275:in `save!'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/factory_girl-4.2.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/factory_girl-4.2.0/lib/factory_girl/evaluation.rb:15:in `[]'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/factory_girl-4.2.0/lib/factory_girl/evaluation.rb:15:in `create'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/factory_girl-4.2.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/factory_girl-4.2.0/lib/factory_girl/strategy/create.rb:9:in `tap'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/factory_girl-4.2.0/lib/factory_girl/strategy/create.rb:9:in `result'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/factory_girl-4.2.0/lib/factory_girl/factory.rb:42:in `run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/factory_girl-4.2.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/notifications.rb:161:in `instrument'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/factory_girl-4.2.0/lib/factory_girl/factory_runner.rb:22:in `run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/factory_girl-4.2.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method'
# ./spec/models/event_dimension_spec.rb:5:in `block (2 levels) in <top (required)>'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `block (2 levels) in let'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `fetch'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `block in let'
# ./spec/models/event_dimension_spec.rb:8:in `block (2 levels) in <top (required)>'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `block (2 levels) in let'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `fetch'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `block in let'
# ./spec/models/event_dimension_spec.rb:10:in `block (2 levels) in <top (required)>'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `block (2 levels) in let'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `fetch'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `block in let'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:433:in `block (2 levels) in its'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `block (2 levels) in let'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `fetch'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:199:in `block in let'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/memoized_helpers.rb:440:in `should'
# ./spec/models/event_dimension_spec.rb:32:in `block (3 levels) in <top (required)>'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example.rb:114:in `instance_eval'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example.rb:114:in `block in run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example.rb:179:in `call'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example.rb:179:in `run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/hooks.rb:87:in `block (2 levels) in run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/hooks.rb:89:in `call'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/hooks.rb:89:in `run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/hooks.rb:427:in `run_hook'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:339:in `run_around_each_hooks'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example.rb:111:in `run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:400:in `block in run_examples'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:396:in `map'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:396:in `run_examples'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:381:in `run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:382:in `block in run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:382:in `map'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:382:in `run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:382:in `block in run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:382:in `map'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/example_group.rb:382:in `run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/command_line.rb:28:in `map'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/command_line.rb:28:in `block in run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/reporter.rb:58:in `report'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/command_line.rb:25:in `run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/runner.rb:80:in `run'
# /Users/kirillberlin/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.4/lib/rspec/core/runner.rb:17:in `block in autorun'