From: francois Date: 2005-08-27T06:34:16+09:00 Subject: Re: probably a stupid rails question Declare the id column as an serial. id serial not null -----Original Message----- From: tsuraan [mailto:tsuraan@gmail.com] Sent: Friday, August 26, 2005 11:11 PM To: ruby-talk ML Subject: probably a stupid rails question I'm working through the Rails intro on http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html, and having a bit of a problem with the part on page three, where you add a recipe to the database. My recipes table (in Postgresql) looks like this:
              Table "public.recipes"
    Column    |          Type          | Modifiers 
--------------+------------------------+-----------
 id           | integer                | not null
 title        | character varying(255) | not null
 description  | character varying(255) | 
 date         | date                   | 
 instructions | text                   | 
Indexes:
    "recipes_pkey" PRIMARY KEY, btree (id)
When I go to add an entry using the Rails structure, I get the following error: > ActiveRecord::StatementInvalid in Recipe#create > > ERROR: null value in column "id" violates not-null constraint > : INSERT INTO recipes ("title", "date", "description", "instructions") VALUES('Cookies', > '2005-08-22', 'Good Stuff', 'I like cookeis a lot. > Mix the ingredient, and cook them. > > yay! > ') > > script/server:49 So, it's looking like Rails isn't properly handling the primary key constraint on the table. Is rails supposed to do this, or should I ditch the primary key in the database? I don't know much about databases or about rails, so I'm probably missing something really obvious. Any ideas? -- No virus found in this incoming message. Checked by AVG Anti-Virus. Version: 7.0.338 / Virus Database: 267.10.15/80 - Release Date: 8/23/2005