gpt4 book ai didi

RSpec、Rails 4、Postgres、UUID 主键 : id is null with Rake + RSpec, 但在 RSpec 或控制台中很好

转载 作者:行者123 更新时间:2023-12-04 18:44:41 26 4
gpt4 key购买 nike

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

哈?

我关注了 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: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我失败了。

编辑:更新标题以反射(reflect) rake + rspec 问题,而不仅仅是 rspec。

最佳答案

我看到了 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)

...运行:
  • 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

    关于RSpec、Rails 4、Postgres、UUID 主键 : id is null with Rake + RSpec, 但在 RSpec 或控制台中很好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17178497/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com