# Inserting to a View - Auto Incrementing Issue

**URL:** <https://www.ruby-forum.com/t/inserting-to-a-view-auto-incrementing-issue/154566>\
**Category:** Rails\
**Created:** [December 1, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/inserting-to-a-view-auto-incrementing-issue/154566 "2008-12-01T18:23:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [December 1, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/inserting-to-a-view-auto-incrementing-issue/154566/1 "2008-12-01T18:23:30Z")

</div>

Hi,

My app has a model defined like this:

MyModel \< ActiveRecord::Base  
set\_table\_name “SOME\_LEGACY\_VIEW”  
set\_primary\_key “LEGACY\_ID”  
end

The Database is DB2.  
The view definition looks like this

SELECT  
LEGACY\_ID, NAME  
FROM LEGACY\_TABLE

When I create a new model and try to save it by doing this…

myModel = MyModel.new  
myModel.name = “TEST”  
myModel.save

Rails is creating an INSERT statement which includes the primary key -  
LEGACY\_ID - set to an ampty String. This blows up auto-increment on  
LEGACY\_TABLE.

Anyone seen this? Suggested solution.

Note - if LEGACY\_ID is removed from the view it works OK, but I’m  
working with requirements that say it should be there.

Thanks.

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [December 2, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/inserting-to-a-view-auto-incrementing-issue/154566/2 "2008-12-02T17:58:00Z")

</div>

Hi Paul,

Could you provide us the description of the LEGACY\_TABLE (what is  
mainly wanted is the type of the column LEGACY\_ID ). Also could you  
capture the insert statement that is being generated and paste it.

Thanks

Praveen

On Dec 1, 10:23 pm, Paul H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
