When I scaffolded my table (non-standard table-name.. standard ''id'' col) the create method dies trying to insert a null value for my primary key. Postgres on Debian.. I experimented with just rewriting the create method, but getting the next id using a find was a bit hackish I thought, and might not handle tons of concurrent users well? Any ideas? Thanks! Kevin http://kevin.is-a-geek.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
develop-U23jnKMpDSxBDgjK7y7TUQ@public.gmane.org
2005-Apr-13 14:04 UTC
Re: scaffold dies on create?
If I recall correctly, the Postgresql adapter assumes you have a generator associated with your id field and that inside the database you have set the column default value to nextval of the generator. I don''t think the adapter tries to put a value in for you. It mearly grabs curval after the insert is done to use as the id value of the object. That would make the null the correct value as far as the adapter is concerned. John W Higgins develop-U23jnKMpDSxBDgjK7y7TUQ@public.gmane.org Quoting Kevin Davis <kgdavis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> When I scaffolded my table (non-standard table-name.. standard ''id'' col) the > create method dies trying to insert a null value for my primary key. > > Postgres on Debian.. > > I experimented with just rewriting the create method, but getting the next > id using a find was a bit hackish I thought, and might not handle tons of > concurrent users well? > > Any ideas? Thanks! > > Kevin > http://kevin.is-a-geek.net >---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
The problem is that it is "illegal" to do a currval() before the sequence is initialized, and that causes an error. I think the recommended way of doing it, is to use a nextval() and do the insert with the value from that call. -asbjxrn On 13 Apr 05, at 10:04 PM, develop-U23jnKMpDSxBDgjK7y7TUQ@public.gmane.org wrote:> If I recall correctly, the Postgresql adapter assumes you have a > generator > associated with your id field and that inside the database you have > set the > column default value to nextval of the generator. I don''t think the > adapter > tries to put a value in for you. It mearly grabs curval after the > insert is > done to use as the id value of the object. > > That would make the null the correct value as far as the adapter is > concerned. > > John W Higgins > develop-U23jnKMpDSxBDgjK7y7TUQ@public.gmane.org > > Quoting Kevin Davis <kgdavis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > >> When I scaffolded my table (non-standard table-name.. standard ''id'' >> col) the >> create method dies trying to insert a null value for my primary key. >> >> Postgres on Debian.. >> >> I experimented with just rewriting the create method, but getting the >> next >> id using a find was a bit hackish I thought, and might not handle >> tons of >> concurrent users well? >> >> Any ideas? Thanks! >> >> Kevin >> http://kevin.is-a-geek.net >> > > > > > ---------------------------------------------------------------- > This message was sent using IMP, the Internet Messaging Program. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- -asbjxrn
Actually it will get initialized on the insert because the default value is a nextval. If the insert fails for whatever reason - there would be other issues to deal with beyond whether or not the currval function didn''t work - which I believe wouldn''t get called anyways because the rails call to currval should not take place after a failed insert. John W Higgins develop-U23jnKMpDSxBDgjK7y7TUQ@public.gmane.org Quoting Asbjørn Bjørnstad <asbjxrn-ltuHofWucvRH4x6Dk/4f9A@public.gmane.org>:> The problem is that it is "illegal" to do a currval() before the > sequence is initialized, and that causes an error. > > I think the recommended way of doing it, is to use a nextval() and do > the insert with the value from that call. > > -asbjxrn > > On 13 Apr 05, at 10:04 PM, develop-U23jnKMpDSxBDgjK7y7TUQ@public.gmane.org wrote: > > > If I recall correctly, the Postgresql adapter assumes you have a > > generator > > associated with your id field and that inside the database you have > > set the > > column default value to nextval of the generator. I don''t think the > > adapter > > tries to put a value in for you. It mearly grabs curval after the > > insert is > > done to use as the id value of the object. > > > > That would make the null the correct value as far as the adapter is > > concerned. > > > > John W Higgins > > develop-U23jnKMpDSxBDgjK7y7TUQ@public.gmane.org > > > > Quoting Kevin Davis <kgdavis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > >> When I scaffolded my table (non-standard table-name.. standard ''id'' > >> col) the > >> create method dies trying to insert a null value for my primary key. > >> > >> Postgres on Debian.. > >> > >> I experimented with just rewriting the create method, but getting the > >> next > >> id using a find was a bit hackish I thought, and might not handle > >> tons of > >> concurrent users well? > >> > >> Any ideas? Thanks! > >> > >> Kevin > >> http://kevin.is-a-geek.net > >> > > > > > > > > > > ---------------------------------------------------------------- > > This message was sent using IMP, the Internet Messaging Program. > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > -asbjxrn > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
On 13 Apr 05, at 10:48 PM, John W Higgins wrote:> Actually it will get initialized on the insert because the default > value is a > nextval. If the insert fails for whatever reason - there would be > other issues > to deal with beyond whether or not the currval function didn''t work - > which I > believe wouldn''t get called anyways because the rails call to currval > should > not take place after a failed insert.You''re right of course. I know I had an issue with this, my simple scaffolded inserts failed with an error code of "not yet defined in this session". I rearranged the insert code in the postgresql code a bit and the error went away. I just arranged it back to the original and the error did not come back. And I can''t find my old logs either, of course. Must have changed something in my table setup. I''m pretty sure I have used SERIAL all the time, but... -- -asbjxrn