5

我正在尝试使用带有 Postgres 和 Rails 4.0.0.rc2 的模型的 UUID 主键启动和运行,但我的规范无法创建和销毁,MyThing.create或者MyThing#destroy在 rails 控制台(在开发和测试中都可以正常工作)环境)。...直到我运行规范,在这种情况下,做这些事情中的任何一个都会停止通过控制台工作。因此,当我运行我的规范改变我的数据库并禁止 UUID 密钥继续工作时,它看起来像发生了一些事情。

哈普?

我按照这篇博客文章生成了我的迁移,因此我的架构如下所示:

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: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) =========================================

创建模型对象:

$ 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">

(高兴)

运行规格:

$ 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", "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'

(悲伤的脸)

有人可以在这里为我指出正确的方向,以了解我的数据库可能会做什么愚蠢的事情吗?

更新:我可以bin/rspec spec一次又一次地成功运行(我的所有规格都通过了)。如果我运行rake spec,我会失败,那么下次我运行时bin/rspec spec会失败。

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

4

2 回答 2

4

我看到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 错误??

更新:我想我会发现它是否是 Rails 错误:问题 11016

于 2013-06-19T17:58:45.113 回答
2

对于其他遇到类似情况的人:

# config/application.rb
config.active_record.schema_format :sql

为了让 postgres 的uuid_ossp扩展正常工作,我需要从 schema.rb 切换到 sql 模式。似乎 schema.rb 试图更加数据库中立,这可以理解地导致自定义数据库扩展的一些不稳定。

见 vikks 评论

于 2014-05-11T14:29:28.103 回答