Note that there are some explanatory texts on larger screens.

plurals
  1. PORSpec, Rails 4, Postgres, UUID primary keys: id is null with Rake + RSpec, but is fine in RSpec or console
    primarykey
    data
    text
    <p>I'm trying to get up and running with UUID primary keys for a model with Postgres and Rails 4.0.0.rc2, but my specs are failing to create and destroy, yet <code>MyThing.create</code> or <code>MyThing#destroy</code> works fine from the rails console (in both dev and test environments). ...until I run specs, in which case doing either of those things stops working via the console. Thus, it looks like something that happens when I run my specs that alters my DB and prohibits UUID keys from continuing to work. </p> <p>Halp?</p> <p>I followed <a href="http://labria.github.io/2013/04/28/rails-4-postgres-uuid-pk-guide/" rel="noreferrer">this blog post</a> to generate my migration, thus my schema looks like:</p> <pre><code>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 </code></pre> <p>Here's the progression of things:</p> <p>Create: <code>$ rake db:create RAILS_ENV=test</code></p> <p>Migrate:</p> <pre><code>$ rake db:migrate RAILS_ENV=test == CreateGrowers: migrating ================================================== -- enable_extension("uuid-ossp") -&gt; 0.0052s -- create_table(:growers, {:id=&gt;:uuid}) -&gt; 0.0043s == CreateGrowers: migrated (0.0096s) ========================================= </code></pre> <p>Create model object:</p> <pre><code>$ rails c test agrian&gt; 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 =&gt; #&lt;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"&gt; </code></pre> <p>(rejoicing)</p> <p>Run specs:</p> <pre><code>$ 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 </code></pre> <p>Create model object:</p> <pre><code>$ rails c test Loading test environment (Rails 4.0.0.rc2) agrian&gt; 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' </code></pre> <p>(sad face)</p> <p>Can someone point me in the right direction here as to what might be doing silly stuff with my db?</p> <p><strong>Update:</strong> I can run <code>bin/rspec spec</code> over and over again with success (all my specs pass). If I run <code>rake spec</code>, I get failures, then the next time I run <code>bin/rspec spec</code> I get failures.</p> <p><strong>Edit:</strong> Updated title to reflect rake + rspec problem, not just with rspec.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload