RSpec、Rails 4、Postgres、UUID主键:id为null,带有Rake+;RSpec,但在RSpec或控制台中没有问题
我正试图为一个带有Postgres和Rails 4.0.0.rc2的模型使用UUID主键启动并运行,但是我的规范未能创建和销毁,但是RSpec、Rails 4、Postgres、UUID主键:id为null,带有Rake+;RSpec,但在RSpec或控制台中没有问题,rspec,ruby-on-rails-4,rails-postgresql,Rspec,Ruby On Rails 4,Rails Postgresql,我正试图为一个带有Postgres和Rails 4.0.0.rc2的模型使用UUID主键启动并运行,但是我的规范未能创建和销毁,但是虚构。create或虚构#destroy可以从Rails控制台(在开发和测试环境中)正常工作。。。在我运行specs之前,在这种情况下,通过控制台执行这些操作中的任何一项都会停止工作。因此,当我运行我的规范改变我的DB并禁止UUID密钥继续工作时,它看起来像是发生了什么 哈尔普 我按照以下步骤生成迁移,因此我的模式如下所示: ActiveRecord::Schema
虚构。create
或虚构#destroy
可以从Rails控制台(在开发和测试环境中)正常工作。。。在我运行specs之前,在这种情况下,通过控制台执行这些操作中的任何一项都会停止工作。因此,当我运行我的规范改变我的DB并禁止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\u 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">
$ 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'
创建模型对象:
$ 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">
$ 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'
(悲伤的脸)
有人能给我指出正确的方向吗?我的db到底在做些什么
更新:我可以成功地反复运行bin/rspec spec
(我所有的spec都通过了)。如果我运行rake spec
,我会失败,然后下次运行bin/rspec spec
我会失败
编辑:更新了标题以反映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\u config
db:test:purge
db:test:load
db:test:load\u schema
db:schema:load
rakedb: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错误:对于其他遇到类似问题的人:
# config/application.rb
config.active_record.schema_format :sql
为了让postgres的uuid_ossp扩展工作,我需要从schema.rb切换到sql模式。似乎schema.rb试图更加独立于数据库,这可以理解,这导致了自定义db扩展的一些奇妙之处
看起来他们将在Rails 4.0.1中修复它。他们应该以你的名字命名发布。