Saturday, July 16, 2005

Ruby on Rails with Postgresql

I've been learning a new system called Ruby on Rails; it's a pure object-oriented programming language with a web application interface. It is supposed to be able to do the kinds of things you get with all the cool google web applications like gmail and google maps. I was following the instructions on and having a difficult time getting it to work, since the instructions are written for MySQL, and not Postgresql. It would come back with an error about column id not honoring the not null constraint. After playing around for a bit, I discovered the solution. Postgres needs to be given a sequence first in order to automatically fill in the id numbers. Here's the SQL code I used to create the database:

create sequence recipes_id_seq start 1;

create table recipes (
  id int primary key default nextval('recipes_id_seq'),
  title varchar(255),
  date date,
  instructions text);
This causes the ID numbers to be inserted correctly.

No comments: