public
Created

ActiveRecord 3.1 cannot load models with non-default PKs unless there's already a database connection

  • Download Gist
Gemfile
Ruby
1 2 3 4
source :rubygems
 
gem 'activerecord', ENV['AR_VERSION'] || '3.1.0'
gem 'sqlite3', '~> 1.3.4'
database.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
require 'active_record'
require 'fileutils'
 
DATABASE_NAME = 'people.sqlite3'
 
FileUtils.rm_rf DATABASE_NAME, :verbose => true
 
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DATABASE_NAME
 
ActiveRecord::Schema.define do
create_table :people, :id => false do |t|
t.primary_key :pid
t.string :name, :null => false
end
end
example.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
require 'bundler'
Bundler.setup
 
require 'active_record'
 
def load_models
puts "Loading models"
require File.expand_path('../models', __FILE__)
end
 
def connect_to_database
puts "Creating and connecting to database"
require File.expand_path('../database', __FILE__)
end
 
def models_first?
ARGV.first =~ /first/
end
 
def msg
"on ActiveRecord #{ActiveRecord::VERSION::STRING} with models loaded #{models_first? ? 'first' : 'second'}"
end
 
begin
puts "== Running #{msg}"
 
if models_first?
load_models
connect_to_database
else
connect_to_database
load_models
end
 
Person.create!(:name => 'Jo')
puts "-- Successful #{msg}!"
rescue => e
puts "-- Failed #{msg}:"
raise e
end
models.rb
Ruby
1 2 3 4 5
require 'active_record'
 
class Person < ActiveRecord::Base
set_primary_key :pid
end
output.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
$ sh ./run.sh
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]
Fetching source index for http://rubygems.org/
Using activesupport (3.0.10)
Using builder (2.1.2)
Using i18n (0.5.0)
Using activemodel (3.0.10)
Using arel (2.0.10)
Using tzinfo (0.3.29)
Using activerecord (3.0.10)
Using sqlite3 (1.3.4)
Using bundler (1.0.15)
Your bundle is updated! Use `bundle show [gemname]` to see where a bundled gem is installed.
== Running on ActiveRecord 3.0.10 with models loaded first
Loading models
Creating and connecting to database
rm -rf people.sqlite3
-- create_table(:people, {:id=>false})
-> 0.0667s
-- Successful on ActiveRecord 3.0.10 with models loaded first!
 
== Running on ActiveRecord 3.0.10 with models loaded second
Creating and connecting to database
rm -rf people.sqlite3
-- create_table(:people, {:id=>false})
-> 0.0700s
Loading models
-- Successful on ActiveRecord 3.0.10 with models loaded second!
 
Fetching source index for http://rubygems.org/
Using multi_json (1.0.3)
Using activesupport (3.1.0)
Using bcrypt-ruby (3.0.0)
Using builder (3.0.0)
Using i18n (0.6.0)
Using activemodel (3.1.0)
Using arel (2.2.1)
Using tzinfo (0.3.29)
Using activerecord (3.1.0)
Using sqlite3 (1.3.4)
Using bundler (1.0.15)
Your bundle is updated! Use `bundle show [gemname]` to see where a bundled gem is installed.
== Running on ActiveRecord 3.1.0 with models loaded first
Loading models
-- Failed on ActiveRecord 3.1.0 with models loaded first:
/Users/rsutphin/.rvm/gems/ruby-1.9.2-p290@tmp/gems/activerecord-3.1.0/lib/active_record/attribute_methods/primary_key.rb:69:in `set_primary_key': undefined method `primary_keys' for nil:NilClass (NoMethodError)
from /private/tmp/ar-cp-pk/models.rb:4:in `<class:Person>'
from /private/tmp/ar-cp-pk/models.rb:3:in `<top (required)>'
from example.rb:8:in `require'
from example.rb:8:in `load_models'
from example.rb:28:in `<main>'
 
== Running on ActiveRecord 3.1.0 with models loaded second
Creating and connecting to database
rm -rf people.sqlite3
-- create_table(:people, {:id=>false})
-> 0.0068s
Loading models
-- Successful on ActiveRecord 3.1.0 with models loaded second!
run.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/bin/sh
 
function run_example {
export AR_VERSION=$1
bundle update
bundle exec ruby example.rb first
echo
bundle exec ruby example.rb second
echo
}
 
echo `ruby -v`
run_example '3.0.10'
run_example '3.1.0'

This is a test case for rails #2807. Clone it, then run run.sh to show that the load order doesn't matter on AR 3.0 but does on AR 3.1.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.