我正在尝试使用带有 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。