# :primary\_key in create\_table with postgresql seems broken

**URL:** <https://www.ruby-forum.com/t/primary-key-in-create-table-with-postgresql-seems-broken/109418>\
**Category:** Rails\
**Created:** [July 14, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/primary-key-in-create-table-with-postgresql-seems-broken/109418 "2007-07-14T15:12:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [July 14, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/primary-key-in-create-table-with-postgresql-seems-broken/109418/1 "2007-07-14T15:12:33Z")

</div>

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:

1. 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.
