I'm working through the Rails intro on Radar – O’Reilly, 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:
<pre>
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)
</pre>
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?
1) It would be better to use the Rails mailing list for your Rails
questions. [1]
2) you need to define a sequence and set the default value of that
column to the nextval of the sequence. This is in PosgreSQL
accomplished by using the 'serial' type when creating the table.
create table foo (id serial primary key, ...);
You can also go back and do it manually by creating a sequence and
altering the table. This is basically equivalent to an
'autoincrement' column in MySQL (not exactly, but similar principle).
I'm working through the Rails intro on Radar – O’Reilly, 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:
<pre>
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)
</pre>
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?
-----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 Radar – O’Reilly, 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:
<pre>
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)
</pre>
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
1) It would be better to use the Rails mailing list for your Rails
questions. [1]
Yeah, I just haven't subscribed yet. I guess I should
2) you need to define a sequence and set the default value of that
column to the nextval of the sequence. This is in PosgreSQL
accomplished by using the 'serial' type when creating the table.
create table foo (id serial primary key, ...);
You can also go back and do it manually by creating a sequence and
altering the table. This is basically equivalent to an
'autoincrement' column in MySQL (not exactly, but similar principle).
On Sat, 2005-08-27 at 06:34 +0900, francois wrote:
foo=# CREATE TABLE recipes (
foot(# id SERIAL PRIMARY KEY,
foo(# name VARCHAR(255),
foo(# created_at TIMESTAMP
foo(# );
NOTICE: CREATE TABLE will create implicit sequence "recipes_id_seq" for serial column "recipes.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "recipes_pkey" for table "recipes"
CREATE TABLE
foo=# \d recipes
Table "public.recipes"
Column | Type | Modifiers
------------+-----------------------------+---------------------------------------------------------
id | integer | not null default nextval('public.recipes_id_seq'::text)
name | character varying(255) |
created_at | timestamp without time zone |
Indexes:
"recipes_pkey" primary key, btree (id)