I am new to Rails and Ruby. I''ve been a WebObjects developer for a few
years
and before that J2EE (shudder). I wanted to try RoR so I am porting an
existing Web app.
I am running the latest release on Tiger and PG8.
Right now my single table has three attributes:
id | integer | not null
hotel_name | character varying(255) | not null
hotel_location | character varying(255) | not null
"hotels_pkey" PRIMARY KEY, btree (id)
When I use the generated insert form, it sends this SQL:
INSERT INTO hotels ("hotel_location", "hotel_name")
VALUES(''Strip'',
''Bellagio'')
Of course there is no PK there so the query fails and an exception is
raised.
I''m following the beta book and haven''t done anything but use
the generator
to get the scaffold.
What am I doing wrong?
Thanks,
Hunter
Hunter Hillegas wrote:>Right now my single table has three attributes: > > id | integer | not null > hotel_name | character varying(255) | not null > hotel_location | character varying(255) | not null > "hotels_pkey" PRIMARY KEY, btree (id) > >When I use the generated insert form, it sends this SQL: > >INSERT INTO hotels ("hotel_location", "hotel_name") VALUES(''Strip'', >''Bellagio'') > >Of course there is no PK there so the query fails and an exception is >raised. > >What am I doing wrong? >Is your id field set to "auto-increment" or whatever the PostgreSQL equivalent is? -- Alex Ezell
I thought that I did set it as ''serial'' when I created the
table but looking
now I see no indication of that in the table definition.
Sure enough, that''s it...
PG wants it to look like this:
create table hotels (
id serial primary key,
hotel_name varchar(255) not null,
hotel_location varchar(255) not null
);
I guess I thought AR might have a PK generator... The beta book specifies
that ''Active Record takes care of creating new primary key values for
records you create...'' without saying how that is done.
Thanks.
> From: Alex Ezell
<taranis-QicVIiZSC4eOfmbybZFjldBPR1lH4CV8@public.gmane.org>
> Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org>
> Date: Thu, 16 Jun 2005 18:57:30 -0400
> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org>
> Subject: Re: [Rails] PostgreSQL Scaffold Doesn''t Insert PK?
>
> Hunter Hillegas wrote:
>
>> Right now my single table has three attributes:
>>
>> id | integer | not null
>> hotel_name | character varying(255) | not null
>> hotel_location | character varying(255) | not null
>> "hotels_pkey" PRIMARY KEY, btree (id)
>>
>> When I use the generated insert form, it sends this SQL:
>>
>> INSERT INTO hotels ("hotel_location", "hotel_name")
VALUES(''Strip'',
>> ''Bellagio'')
>>
>> Of course there is no PK there so the query fails and an exception is
>> raised.
>>
>> What am I doing wrong?
>>
> Is your id field set to "auto-increment" or whatever the
PostgreSQL
> equivalent is?
>
> --
> Alex Ezell
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
i think it is... change the id to hotel_id or have you model tell actionrecord that the primary key is named id instead of hotel_id see primary_key at api.rubyonrails.com but to make my life easier i never did just ''id'' as everyhting i''ve done with rails so far is from scratch... Hunter Hillegas wrote:>I am new to Rails and Ruby. I''ve been a WebObjects developer for a few years >and before that J2EE (shudder). I wanted to try RoR so I am porting an >existing Web app. > >I am running the latest release on Tiger and PG8. > >Right now my single table has three attributes: > > id | integer | not null > hotel_name | character varying(255) | not null > hotel_location | character varying(255) | not null > "hotels_pkey" PRIMARY KEY, btree (id) > >When I use the generated insert form, it sends this SQL: > >INSERT INTO hotels ("hotel_location", "hotel_name") VALUES(''Strip'', >''Bellagio'') > >Of course there is no PK there so the query fails and an exception is >raised. > >I''m following the beta book and haven''t done anything but use the generator >to get the scaffold. > >What am I doing wrong? > >Thanks, >Hunter > > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails