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..
Please post your the model file and the migration or sql file for the model you are having problems with. On 4/18/06, zven <zven@nm.ru> wrote:> > 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.. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060418/7e7753ac/attachment.html
Which PostgreSQL driver and version of PostgreSQL are you using? I am using the ruby gem with PostgreSQL 8.1 on Windows XP and haven''t had any problems. The main difference is that I am using generated scaffolds instead of run-time scaffolds. Have you tried both? Also, the migration you posted below doesn''t quite match your schema dump. Are they out of sync? Please be sure to post back to the list so others can contribute and learn. On 4/18/06, zven <zven@nm.ru> wrote:> > On Tue, 2006-04-18 at 09:42 -0700, Eden Brandeis wrote: > > Please post your the model file and the migration or sql file for the > > model you are having problems with. > > > > On 4/18/06, zven <zven@nm.ru> wrote: > > 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.. > > > > 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. > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060418/b98b2808/attachment.html
On Tue, 2006-04-18 at 09:42 -0700, Eden Brandeis wrote:> Please post your the model file and the migration or sql file for the > model you are having problems with. > > On 4/18/06, zven <zven@nm.ru> wrote: > 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.. >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.
On Apr 18, 2006, at 10:39 PM, zven wrote:> 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'')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 Mornini
On Tue, 2006-04-18 at 11:53 -0700, Tom Mornini wrote:> On Apr 18, 2006, at 10:39 PM, zven wrote: > > > 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'') > > 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.
zven wrote:> On Tue, 2006-04-18 at 11:53 -0700, Tom Mornini 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. -- Posted via http://www.ruby-forum.com/.