Thursday, December 13, 2007

Ruby, Postgres and Primary keys

Started working on one of my first ruby projects. The first issue I have run into deals with keys for a table. In my designs I had a primary key that I used. It was causing an error message when trying to create a record in the table.

User Create (0.000000) RuntimeError: ERRORC23502Mnull value in column "user_id" violates not-null constraintFexecMain.cL1818RExecConstraints: INSERT INTO users ("end_date", "last_login", "name", "salt", "start_date", "hashed_password", "last_session", "user_id") VALUES(NULL, NULL, 'user', NULL, NULL, NULL, NULL, NULL)

I couldn't find anything that would show me how to not include a field in the automatically generated SQL by ruby. I did notice after looking at my table that ruby had created it's own primary key for the table and that it isn't included in the auto generated SQL.

Solution roll back to a previous version of the database that didn't have my new user table remove the column definition in the db\migrate\002_create_user_table. Then migrate back up to the current version.

rake db:migrate VERSION=

I do intend to find out how to override values, if they can be overridden.

Labels:

2 Comments:

At 1:51 AM, February 16, 2008 , OpenID kfsone said...

Ruby is just a language, I'm guessing you're talking about Rails. If so, when I looked at Rails it seemed like its default name for the id field was ... "id". You can override the field-name definitions somehow but it involved creating an extra file specific to the task, for every table, which felt so much like having to fight the thing that I deleted the install and never looked back.

I still swear by Roxen - www.roxen.com. There's a commercial version with a built in Content Management System but if you look carefully, Roxen itself is free and includes RXML, the XML-based scripting language that some argue JSP Tags is based on.

 
At 7:26 AM, February 16, 2008 , Blogger Breed said...

Yes, rails. I guess it is really a function of active record. I stopped rails development and have focused on ruby itself before going back in.

 

Post a Comment

Links to this post:

Create a Link

<< Home