Andrew Madu
2007-Mar-02 03:22 UTC
SOLVED: Re: PostgreSQL primary (sequence) key issue (Ruby/Rails)
Hi, this one has been finally put to bed!! -- Regards Andrew On 01/03/07, Andrew Madu <andrewmadu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi Guys, > I''m completely stumped with this one! > > I''ve included ActiveRecord::Base.set_sequence_name in my environment.rb, > and in my user.rb I''ve placed the following code: > > class Usertbl < ActiveRecord::Base > set_primary_key "user_id" > set_sequence_name "seq_user_mytable" > validates_uniqueness_of :userpassword, :username, :scope => :user_id > end > > From http://localhost:3000/usertbl/new I then attempt to enter a new a > record and receive the following error message: > > > RuntimeError: ERROR C23502 Mnull value in column "user_id" violates not-null constraint > > > FexecMain.c L1795 RExecConstraints: INSERT INTO usertbl > > > > Oh my head hurts!! > > -- > Regards > > Andrew > > > On 01/03/07, Andrew Madu < andrewmadu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi Dave, > > my apologies for contacting you off list but i''m having a spot of bother > > with postgreSQL sequence setup in rails. In addition to what is mentioned > > below, I have place the following line of code in my environment.rbdocument: > > > > # Include your application configuration below > > ActiveRecord::Base.pluralize_table_names = false > > ActiveRecord::Base.set_sequence_name > > > > What I am overlooking here? > > > > -- > > Regards > > > > Andrew > > > > ---------- Forwarded message ---------- > > From: Andrew Madu < andrewmadu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > Date: 01-Mar-2007 15:28 > > Subject: Re: PostgreSQL primary (sequence) key issue > > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > > Hi, > > I''ve followed the example given here: > > > > http://ar.rubyonrails.com/classes/ActiveRecord/Base.html#M000367 > > > > and implemented set_sequence_name "seq_name" in my class. When I try to > > update the user table with a new row from: > > > > http://localhost:3000/usertbl/new > > > > I get the following error message: > > > > RuntimeError: ERROR C23502 Mnull value in column "user_id" violates not-null constraint > > > > > > > > FexecMain.c L1795 RExecConstraints: INSERT INTO usertbl > > > > > > What am I overlooking here? > > > > Also coud possibly give me any clues on how to setup an ''assigned'' > > primary key in an ActiveRecord class? > > > > -- > > Regards > > > > Andrew > > > > On 01/03/07, Andrew Madu <andrewmadu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Hi, > > > I have a sequence set up on one of my tables called seq_user_mytable. > > > I tried the following with no joy: > > > > > > set_primary_key "user_id", :sequence => "seq_user_mytable" and: > > > > > > ActiveRecord:: Base.seq_user_mytable > > > > > > again with no joy. How do I implement a sequence in > > > Rails/ActiveRecord? > > > > > > -- > > > Regards > > > > > > Andrew > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeremy Kemper
2007-Mar-02 03:54 UTC
Re: SOLVED: Re: PostgreSQL primary (sequence) key issue (Ruby/Rails)
On 3/1/07, Andrew Madu <andrewmadu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > this one has been finally put to bed!!How? Curious, jeremy --> Regards > > Andrew > > On 01/03/07, Andrew Madu <andrewmadu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > wrote: > > > > Hi Guys, > > I''m completely stumped with this one! > > > > I''ve included ActiveRecord:: Base.set_sequence_name in my environment.rb, > > and in my user.rb I''ve placed the following code: > > > > class Usertbl < ActiveRecord::Base > > set_primary_key "user_id" > > set_sequence_name "seq_user_mytable" > > validates_uniqueness_of :userpassword, :username, :scope => :user_id > > end > > > > From http://localhost:3000/usertbl/new I then attempt to enter a new a > > record and receive the following error message: > > > > > > RuntimeError: ERROR C23502 Mnull value in column "user_id" violates not-null constraint > > > > > > > > FexecMain.c L1795 RExecConstraints: INSERT INTO usertbl > > > > > > > > Oh my head hurts!! > > > > -- > > Regards > > > > Andrew > > > > > > On 01/03/07, Andrew Madu < andrewmadu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Hi Dave, > > > my apologies for contacting you off list but i''m having a spot of > > > bother with postgreSQL sequence setup in rails. In addition to what is > > > mentioned below, I have place the following line of code in my > > > environment.rb document: > > > > > > # Include your application configuration below > > > ActiveRecord::Base.pluralize_table_names = false > > > ActiveRecord::Base.set_sequence_name > > > > > > What I am overlooking here? > > > > > > -- > > > Regards > > > > > > Andrew > > > > > > ---------- Forwarded message ---------- > > > From: Andrew Madu < andrewmadu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > Date: 01-Mar-2007 15:28 > > > Subject: Re: PostgreSQL primary (sequence) key issue > > > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > > > > Hi, > > > I''ve followed the example given here: > > > > > > http://ar.rubyonrails.com/classes/ActiveRecord/Base.html#M000367 > > > > > > and implemented set_sequence_name "seq_name" in my class. When I try > > > to update the user table with a new row from: > > > > > > http://localhost:3000/usertbl/new > > > > > > I get the following error message: > > > > > > RuntimeError: ERROR C23502 Mnull value in column "user_id" violates not-null constraint > > > > > > > > > > > > > > > FexecMain.c L1795 RExecConstraints: INSERT INTO usertbl > > > > > > > > > What am I overlooking here? > > > > > > Also coud possibly give me any clues on how to setup an ''assigned'' > > > primary key in an ActiveRecord class? > > > > > > -- > > > Regards > > > > > > Andrew > > > > > > On 01/03/07, Andrew Madu <andrewmadu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > Hi, > > > > I have a sequence set up on one of my tables called > > > > seq_user_mytable. I tried the following with no joy: > > > > > > > > set_primary_key "user_id", :sequence => "seq_user_mytable" and: > > > > > > > > ActiveRecord:: Base.seq_user_mytable > > > > > > > > again with no joy. How do I implement a sequence in > > > > Rails/ActiveRecord? > > > > > > > > -- > > > > Regards > > > > > > > > Andrew > > > > > > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Madu
2007-Mar-02 17:04 UTC
Re: SOLVED: Re: PostgreSQL primary (sequence) key issue (Ruby/Rails)
Hi, How?>the process is as follows (i''m using pgRails by the way): 1. In project/config/environment.rb place the following line: # Include your application configuration below - Located at bottom of page ActiveRecord::Base.pluralize_table_names = false 2. In C:\pgRails\lib\ruby\gems\1.8\gems\activerecord- 1.15.2\lib\active_record\connection_adapters\postgresql_adapter.rb place the following code: Note: You are looking for: module ConnectionAdapters ..... class PostgreSQLAdapter < AbstractAdapter then place the following code: #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def prefetch_primary_key?(table_name = nil) true end def next_sequence_value(sequence_name) Integer(select_value("SELECT nextval(''#{sequence_name}'')")) end This def next_sequence_value, alternatively, would be place in your MySQL adapter file if you were using MySQL!! #def next_sequence_value(sequence_name) # sql = "UPDATE #{sequence_name} SET Id=LAST_INSERT_ID(Id+1);" # update(sql, "#{sequence_name} Update") # select_value("SELECT Id from #{ sequence_name}",''Id'') #end #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3. In C:\pgRails\lib\ruby\gems\1.8\gems\activerecord- 1.15.2\lib\active_record\base.rb place the following code: Note: You are looking to replace/comment out def reset_sequence_name then replace that block with this: def reset_sequence_name "#{table_name}_seq" end 4. In your model class (for example) class Usertbl < ActiveRecord::Base set_primary_key "your_tbl_id" set_sequence_name "your_db_sequence" #Note that your dB sequence will have to have the format of somename_seq end That''s it, ALL DONE!! Hope that helps -- Regards Andrew --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---