Probably a stupid rails question

(tsuraan) #1

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:

<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?

(Jason Foreman) #2

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

Jason

[1] http://lists.rubyonrails.org/mailman/listinfo/rails

···

On 8/26/05, tsuraan <tsuraan@gmail.com> wrote:

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:

<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?

(Francois Paul) #3

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:

<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

(tsuraan) #4

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 :slight_smile:

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

Cool, thanks!

(Robby Russell) #5

Declare the id column as an serial.

id serial not null

Wait, don't forget the PRIMARY KEY!

not null is automatically done..

···

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)

--
/******************************************************
* Robby Russell, Owner.Developer.Geek
* PLANET ARGON, Open Source Solutions & Web Hosting
* Portland, Oregon | p: 503.351.4730 | f: 815.642.4068
* www.planetargon.com | www.robbyonrails.com
*******************************************************/