My model has:
class CreateSamples < ActiveRecord::Migration
def self.up
create_table :samples, :primary_key => :other_id do |t|
t.column :name, :string
end
end
def self.down
drop_table :samples
end
end
my unit test has:
require File.dirname(FILE) + ‘/…/test_helper’
class SampleTest < Test::Unit::TestCase
Replace this with your real tests.
def test_save
sample = Sample.new
sample.save
end
end
It fails.
If I get into script/console, the “keys” include the primary key but I
believe it should not:
sample = Sample.new
sample = Sample.new
=> #<Sample other_id: nil, name: nil>puts sample.attributes.keys
puts sample.attributes.keys
name
other_id
=> nil
The error from the test is:
- Error:
test_save(SampleTest):
ActiveRecord::StatementInvalid: PGError: ERROR: null value in column
“other_id” violates not-null constraint
: INSERT INTO samples (“name”, “other_id”) VALUES(NULL, NULL)
/Users/pedz/rails-projects/pk/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:135:in
log' /Users/pedz/rails-projects/pk/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:160:in
execute’
Notice that other_id is being specified in the insert instead of just
letting it default to the auto sequence.
Is this going to be a problem in the postgresql adapter? I’d like to
track this down but so far, I’m not getting anywhere.