# Set\_primary\_key issue

**URL:** <https://www.ruby-forum.com/t/set-primary-key-issue/92219>\
**Category:** Rails\
**Created:** [April 17, 2007, 6:52am UTC](https://www.ruby-forum.com/t/set-primary-key-issue/92219 "2007-04-17T06:52:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jefflin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jefflin](https://www.ruby-forum.com/u/jefflin)\
**Post date:** [April 17, 2007, 6:52am UTC](https://www.ruby-forum.com/t/set-primary-key-issue/92219/1 "2007-04-17T06:52:26Z")

</div>

Hi:

I am new to the ruby and rails. What I am tried to do is to create a  
scaffold based on an existing table in a MS SQL Server database.

Before I used the set\_primary\_key method, edit and show does not work  
in the generated code. Rails displayed the list of the records ok, but  
can not determine the id for each record. Create a new record does  
work properly, however.

After I used the set\_primary\_key method to set the right primary key  
(Uesr\_ID), rails can now determine the id for each records when list  
of the records is displayed. But update does not work properly.  
looking at the development log, the update sql statement generated  
included all fields except the primary key field under Set cause.  
Creating a new record, also generated similar sql insert statement.  
The primary key is not included. BTW, for new record function to work,  
I have to modify the controller’s new method to initialise the id  
field, otherwise I will receive "undefined method  
`UserID\_before\_type\_cast’ " exception.

Look back to the development log, before the set\_primary\_key method is  
used, generated insert statement for the new record does include all  
fields.

To me, it seems that the activerecord has excluded the primary key  
field when generating update and insert statement when the  
set\_primary\_key method is used.

Can anyone point me to the right direction to resolve this issue?

Thanks.
