need help fixing unique key in rails. rails is adding id causing duplicate key
        Posted  
        
            by railsnew
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by railsnew
        
        
        
        Published on 2010-04-25T23:24:00Z
        Indexed on 
            2010/04/25
            23:33 UTC
        
        
        Read the original article
        Hit count: 340
        
rails
|activerecord
I need some help in fixing the below issue. I had transaction blocks in my rails code like below:
@sqlcontact =  "INSERT INTO contacts (id,\"cid\", \"hphone\", mphone, provider, cemail, email, sms , mail, phone) VALUES ('"+@id1+"','" + @id1  + "', '"+ params[:hphone] + "', '"+params[:mphone]+ "', '" + params[:provider] + "', '" + params[:cemail]+ "', '" +      @varemail+ "', '"+@varsms+ "', '"+ @varmail+"', '"+@varphone+"')"
my app was deployed to heroku so I was advised by them to remove transaction blocks. So I changed the above to:
@cont = Contact.new(:id => @id1, :cid => @id1, :hphone => params[:hphone], :mphone => params[:mphone], :provider => params[:provider], :cemail => params[:cemail], :email => @varemail, :sms => @varsms, :mail => @varmail, :phone => @varphone)
@cont.save
My app also already had data stored.
Now the problem is that when I try to save a record ...I keep getting the error:
duplicate key value violates unique constraint "contacts_pkey"
The error also shows the sql query trying to insert data ...however, in that sql query i Do not see id value.  As you can see from my code that I am passing the id. then why is rails not accepting it? does it always include its own sequential id? can I not overwrite the default rails magic? and if it does that...does it not look at data that is already in the DB??
I am really stuck here. What should I do? should I just go back to my transaction block
© Stack Overflow or respective owner