我正在尝试使用UgID主键来运行Postgres和Rails 4.0.0.rc2的模型,但我的规格无法创建和销毁,但MyThing.create或MyThing#destroy在rails控制台上工作正常(在开发和测试环境中). …直到我运行规范,在这种情况下,通过控制台停止工作.因此,当我运行我的规范改变我的数据库并禁止UUID密钥继续工作时,它看起来就像发生了什么.
HALP?
我按照this blog post生成了我的迁移,因此我的模式如下所示:
ActiveRecord::Schema.define(version: 20130613174601) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" enable_extension "uuid-ossp" create_table "growers",id: false,force: true do |t| t.uuid "id",null: false t.string "name" t.string "code" t.datetime "created_at" t.datetime "updated_at" end end
这是事物的进展:
创建:
$rake db:创建RAILS_ENV = test
迁移:
$rake db:migrate RAILS_ENV=test == CreateGrowers: migrating ================================================== -- enable_extension("uuid-ossp") -> 0.0052s -- create_table(:growers,{:id=>:uuid}) -> 0.0043s == CreateGrowers: migrated (0.0096s) =========================================
创建模型对象:
$rails c test agrian> g = Grower.create name: 'bobo' (0.3ms) BEGIN sql (4.1ms) INSERT INTO "growers" ("created_at","name","updated_at") VALUES ($1,$2,$3) RETURNING "id" [["created_at",Tue,18 Jun 2013 20:22:39 UTC +00:00],["name","bobo"],["updated_at",18 Jun 2013 20:22:39 UTC +00:00]] (0.4ms) COMMIT => #<Grower id: "38f84f39-e52e-4664-b776-4fdfcbd60b09",name: "bobo",code: nil,created_at: "2013-06-18 20:22:39",updated_at: "2013-06-18 20:22:39">
(大喜)
运行规格:
$rake spec /Users/sloveless/.rbenv/versions/2.0.0-p195/bin/ruby -S rspec ./spec/controllers/api/v1/growers_controller_spec.rb ./spec/helpers/growers_helper_spec.rb ./spec/models/grower_spec.rb ./spec/requests/api/v1/growers_spec.rb ./spec/routing/api/v1/growers_routing_spec.rb ...............FF..........F.* (other stuff) Finished in 0.30626 seconds 30 examples,3 failures,1 pending
创建模型对象:
$rails c test Loading test environment (Rails 4.0.0.rc2) agrian> g = Grower.create name: 'bobo' (0.4ms) BEGIN sql (3.5ms) INSERT INTO "growers" ("created_at",$3) [["created_at",18 Jun 2013 20:29:36 UTC +00:00],18 Jun 2013 20:29:36 UTC +00:00]] PG::Error: ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null,bobo,null,2013-06-18 20:29:36.773391,2013-06-18 20:29:36.773391). : INSERT INTO "growers" ("created_at",$3) (0.2ms) ROLLBACK ActiveRecord::StatementInvalid: PG::Error: ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null,$3) from /Users/sloveless/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/connection_adapters/postgresql_adapter.rb:780:in `get_last_result'
(悲伤的脸)
有人可以指出我在这方面正确的方向,我的数据库可能做什么愚蠢的事情?
更新:我可以一遍又一遍地运行bin / rspec规范(所有我的规范都通过).如果我运行rake规范,我会失败,然后下次运行bin / rspec规范时我会失败.
编辑:更新标题以反映rake rspec问题,而不仅仅是rspec.
解决方法
我看到rspec-rails / lib / rspec / rails / tasks / rspec.rake为spec任务定义了这个:
spec_prereq = Rails.configuration.generators.options[:rails][:orm] == :active_record ? "test:prepare" : :noop task :noop do; end task :default => :spec # other stuff desc "Run all specs in spec directory (excluding plugin specs)" RSpec::Core::RakeTask.new(:spec => spec_prereq)
…运行:
> db:load_config
> db:test:purge
> db:test:load
> db:test:load_schema
> db:schema:load
我看到,当运行rake db:schema:load时,我得到:
-- enable_extension("plpgsql") -> 0.0161s -- enable_extension("uuid-ossp") -> 0.0063s -- create_table("growers",{:id=>false,:force=>true}) -> 0.0049s -- initialize_schema_migrations_table() -> 0.0062s
…与运行迁移时不同:
== CreateGrowers: migrating ================================================== -- enable_extension("uuid-ossp") -> 0.0050s -- create_table(:growers,{:id=>:uuid}) -> 0.0052s == CreateGrowers: migrated (0.0103s) =========================================
因此,在我看来,db:schema:load任务不会创建具有UUID主键的表,从而导致失败.好像是一个Rails bug?
更新:我想我会发现它是否是一个Rails错误:Issue 11016
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。