# Postgresql and ActiveRecords problems

**URL:** <https://www.ruby-forum.com/t/postgresql-and-activerecords-problems/58121>\
**Category:** Rails\
**Created:** [April 18, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/postgresql-and-activerecords-problems/58121 "2006-04-18T18:37:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 18, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/postgresql-and-activerecords-problems/58121/1 "2006-04-18T18:37:36Z")

</div>

Hi all. I have a problem with postgresql PK columns and ActiveRecord.  
The error is: PGError: ERROR: null value in column “item\_id” violates  
not-null.

Ok, it’s wrong to insert NULL into PK columns, but rails doing it. How  
to fix? So sad…

---

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 18, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/postgresql-and-activerecords-problems/58121/2 "2006-04-18T18:43:53Z")

</div>

Please post your the model file and the migration or sql file for the  
model  
you are having problems with.

---

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 18, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/postgresql-and-activerecords-problems/58121/3 "2006-04-18T20:40:47Z")

</div>

On Tue, 2006-04-18 at 09:42 -0700, Eden B. wrote:

> ```
> Ok, it's wrong to insert NULL into PK columns, but rails doing
> it. How
> to fix? So sad..
> 
> ```

I am using Debian with rails-1.1.0 (installed by apt-get), postgresql  
8.1

Sql schema:

create table sections  
(  
section\_id serial,  
name varchar(25) not null,  
title varchar(25),  
primary key(section\_id)  
);

class SectionController \< ApplicationController  
scaffold :section  
end

class Section \< ActiveRecord::Base  
end

class CreateSections \< ActiveRecord::Migration  
def self.up  
create\_table :sections do |t|  
# t.column :name, :string  
end  
end

def self.down  
drop\_table :sections  
end  
end

‘NULL’ wrong here because it’s must be omitted to become a correct  
postgres sql query. Correct is: INSERT INTO sections (“name”, “title”)  
VALUES(‘sdfdsfd’, ‘sfsdfsd’)

T think it’s a bug in activerecord ostgresql driver.

---

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 18, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/postgresql-and-activerecords-problems/58121/4 "2006-04-18T21:05:30Z")

</div>

On Tue, 2006-04-18 at 11:53 -0700, Tom M. wrote:

> > );  
> > create\_table :sections do |t|  
> > postgres sql query. Correct is: INSERT INTO sections (“name”, “title”)  
> > VALUES(‘sdfdsfd’, ‘sfsdfsd’)
> 
> Primary keys in Rails are assumed to be named ‘id’
> 
> It can be overridden for legacy schema, but if you do override the  
> column  
> name you still access the value as an attribute named ‘id’

I know it already ). Problem solved! Thanks to all.

---

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 18, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/postgresql-and-activerecords-problems/58121/5 "2006-04-18T20:56:15Z")

</div>

On Apr 18, 2006, at 10:39 PM, zven wrote:

> class SectionController \< ApplicationController  
> end  
> end
> 
> def self.down  
> drop\_table :sections  
> end  
> end
> 
> ‘NULL’ wrong here because it’s must be omitted to become a correct  
> postgres sql query. Correct is: INSERT INTO sections (“name”, “title”)  
> VALUES(‘sdfdsfd’, ‘sfsdfsd’)

Primary keys in Rails are assumed to be named ‘id’

It can be overridden for legacy schema, but if you do override the  
column  
name you still access the value as an attribute named ‘id’

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [April 18, 2006, 11:49pm UTC](https://www.ruby-forum.com/t/postgresql-and-activerecords-problems/58121/6 "2006-04-18T23:49:50Z")

</div>

zven wrote:

> On Tue, 2006-04-18 at 11:53 -0700, Tom M. wrote:
> 
> > > );  
> > > create\_table :sections do |t|  
> > > postgres sql query. Correct is: INSERT INTO sections (“name”, “title”)  
> > > VALUES(‘sdfdsfd’, ‘sfsdfsd’)
> > 
> > Primary keys in Rails are assumed to be named ‘id’
> > 
> > It can be overridden for legacy schema, but if you do override the  
> > column  
> > name you still access the value as an attribute named ‘id’
> 
> I know it already ). Problem solved! Thanks to all.

You should still consider upgrading your rails 1.1.0 to rails 1.1.2.  
You’ll benefit from numerous bugfixes without introducing unstable new  
features.
