RSpec,Rails 4,Postgres,UUID主键:id为Rake + RSpec为null,但在RSpec或控制台中没问题

tur*_*den 5 rspec rails-postgresql ruby-on-rails-4

我正在尝试使用UgID主键来运行Postgres和Rails 4.0.0.rc2的模型,但是我的规格无法创建和销毁,但是MyThing.create还是MyThing#destroy在rails控制台上工作正常(在开发和测试中都是如此)环境)....直到我运行规范,在这种情况下,通过控制台停止工作.因此,当我运行我的规范改变我的数据库并禁止UUID密钥继续工作时,它看起来就像发生了什么.

HALP?

我按照这篇博客文章来生成我的迁移,因此我的架构看起来像:

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
Run Code Online (Sandbox Code Playgroud)

这是事物的进展:

创建: $ rake db:create 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) =========================================
Run Code Online (Sandbox Code Playgroud)

创建模型对象:

$ 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", Tue, 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">
Run Code Online (Sandbox Code Playgroud)

(大喜)

运行规格:

$ 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
Run Code Online (Sandbox Code Playgroud)

创建模型对象:

$ 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", "name", "updated_at") VALUES ($1, $2, $3)  [["created_at", Tue, 18 Jun 2013 20:29:36 UTC +00:00], ["name", "bobo"], ["updated_at", Tue, 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", "name", "updated_at") VALUES ($1, $2, $3)
   (0.2ms)  ROLLBACK
ActiveRecord::StatementInvalid: 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", "name", "updated_at") VALUES ($1, $2, $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'
Run Code Online (Sandbox Code Playgroud)

(悲伤的脸)

有人可以指出我在这方面正确的方向,我的数据库可能做什么愚蠢的事情?

更新:我可以bin/rspec spec一次又一次地成功运行(我的所有规格都通过).如果我跑rake spec,我会失败,然后下次我跑,bin/rspec spec我得到失败.

编辑:更新标题以反映rake + rspec问题,而不仅仅是rspec.

tur*_*den 4

我看到它rspec-rails/lib/rspec/rails/tasks/rspec.rakespec任务定义了这一点:

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)
Run Code Online (Sandbox Code Playgroud)

...运行:

  • 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
Run Code Online (Sandbox Code Playgroud)

...这与我运行迁移时不同:

==  CreateGrowers: migrating ==================================================
-- enable_extension("uuid-ossp")
   -> 0.0050s
-- create_table(:growers, {:id=>:uuid})
   -> 0.0052s
==  CreateGrowers: migrated (0.0103s) =========================================
Run Code Online (Sandbox Code Playgroud)

因此,在我看来,该db:schema:load任务并未使用 UUID 主键创建表,从而导致失败。看起来像是 Rails 的错误??

更新:我想我会找出这是否是 Rails 错误:Issue 11016

  • 看起来他们将在 Rails 4.0.1 中修复它。他们应该以您的名字命名该版本。 (2认同)