Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord no longer populates uuid ids that were generated in the database

#5205

I have an app with a postgres database. My primary keys are uuids that are populated by a postgres function. In Rails 2.3.8 when I create a record, the id field gets populated. In Rails RC the create succeeds, the id is not populated and the object is left in a very strange state.

Reported by Jeff Dean · July 27th, 2010 @ 02:46 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    Can you post your migration, model and the operation of creating the object.

    July 27th, 2010 @ 04:05 AM

  2. Jeff Dean
    Jeff Dean

    Migration

    class CreatePosts < ActiveRecord::Migration
      def self.up
        execute <<-SQL
          CREATE FUNCTION uuid_generate_v1() RETURNS uuid
              LANGUAGE c STRICT
              AS '$libdir/uuid-ossp', 'uuid_generate_v1';
        
          CREATE TABLE posts (
              id character varying(36) DEFAULT uuid_generate_v1() NOT NULL,
              title character varying
          );
        SQL
      end
    
      def self.down
        drop_table :posts
      end
    end
    

    Model

    class Post < ActiveRecord::Base
    end
    

    Test

    require 'test_helper'
    
    class PostTest < ActiveSupport::TestCase
      test "save should be true" do  # passes
        post = Post.new :title => "Foo"
        assert post.save, "Post save should be true"
      end
    
      test "save! should be true" do  # passes
        post = Post.new :title => "Foo"
        assert post.save!, "Post save should be true"
      end
    
      test "save! should change the number of records" do  # passes
        assert_difference('Post.count', 1) do
          Post.create! :title => "Foo"
        end
      end
    
      test "save assigns the id" do # fails!
        post = Post.create! :title => "Foo"
        assert post.id.present?, "Post should have an id"
      end
    end
    

    Gemfile

    source 'http://rubygems.org'
    gem 'rails', '3.0.0.rc'
    gem 'pg'
    

    I've also attached a zip file of the app that might be easier to use to reproduce. I haven't taken the time to see whether this is broken in other adapters.

    I've confirmed that on a brand-new rails 2.3.8 app with an identical migration all tests pass. I've also included a zip file with that example.

    July 27th, 2010 @ 05:18 AM

  3. Jeff Dean
  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:35 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:35 PM