My Ruby on Rails system is moving from Oracle to Microsoft SQL Server 2012. The back end database has already been converted by a third part from Oracle to Microsoft SQL Server. I have no control over the schema structure. This cannot be changed.
Using activerecord-sqlserver-adapter, tiny_tds and Freetds I can connect to the new database. Most DB requests work well.
However, many of the tables have ID primary keys that are auto incremented by SQL Server, in other words, PK IDENTITY(1,1) columns. The same code that worked with Oracle connections fails under SQL Server with the following error:
TinyTds::Error: Cannot insert the value NULL into column 'ID'
I know why it is doing this as the ID column is indeed a primary_key, and IDENTITY(1,1) column and includes the NOT NULL restriction. That is fine.
Inserting into the table works fine if using raw SQL execute statements where of course I exclude the ID column from the INSERT statement.
However I have spent days googling and I cannot find a way of telling Ruby on Rails not to try and save the ID column.
So I have an instance of a class, @book, that contains
Library::Book(id: integer, title: string, isbn: string ...)
When I do a @book.save! it generates the error above.
@book = Library::Book.new( .. )
:
:
@book.save!
TinyTds::Error: Cannot insert the value NULL into column 'ID'
Rather that resort to bare metal SQL, how do I do things more Railsy and tell it I want to save the record but not try and save the ID field as it is auto incremented? So effectively I am trying to save
Library::Book(title: string, isbn: string ...) if a new insert entry or Library::Book(id: integer, title: string, isbn: string ...) if trying to update an entry.
Due to imposed restrictions I am using: Ruby 2.3.3p222 Rails 4.0.13 activerecord (4.0.13) activerecord-sqlserver-adapter (4.0.4) tiny_tds (1.0.2) Freetds 1.00.27