rails-sqlserver / activerecord-sqlserver-adapter

SQL Server Adapter For Rails
MIT License
972 stars 558 forks source link

returned id of created objects is wrong #160

Closed timsjoberg closed 12 years ago

timsjoberg commented 12 years ago

I don't know if this is a peculiarity of the database I'm connecting to, or if it's a bug in the sqlserver adapter, but some insight would be great.

I'm attempting to migrate data from a sqlserver database to a postgres database. The schemas are not the same. In my testing, I'm trying to check that associations are working as expected on the sqlserver models. I'm using activerecord 3.0.11 as associations are broken in activerecord 3.1 when connecting to multiple databases ( https://t.co/FIXFfAnG ). I'm using sqlserver adapter version 3.0.19. This is the behaviour I'm experiencing:

random_string = SecureRandom.hex(16)
country = Host::Country.create!(:Country => random_string)
p country
# => #<Host::Country CountryID: 19, Country: "298ae41bec0aa4167eeaf0ac8faaef4c">
p Host::Country.find_by_Country(random_string)
# => #<Host::Country CountryID: 278, Country: "298ae41bec0aa4167eeaf0ac8faaef4c">

The model looks something like this (where HostDatabase is an abstract class inheriting from AR::Base and establishing the connection correctly):

module Host
  class Country < HostDatabase
    set_table_name 'Countries'
    set_primary_key 'CountryID'
  end
end

It seems to always give 19 back in this scenario. Is it attempting to use a sequence and not doing it properly? (if this was the case, surely it should be going up?). Or is this a legitimate bug?

timsjoberg commented 12 years ago

Update: it seems to always give back 19, including on other tables. So almost certainly not a sequence thing

metaskills commented 12 years ago

I think you have a config problem and I really wish I would remember what it is, but 19 as the identity is the dead give away. Please do a little bit of research into past tickets and the google group to see if you can find the answer.

https://github.com/rails-sqlserver/tiny_tds/issues/19 https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/wiki/Using-TinyTds

timsjoberg commented 12 years ago

Thanks very much. You are right

The problem was the tds version specified in the freetds.conf file. Because Ubuntu has compiled the default version to be 4.2, I was getting errors with tables with certain data types. I (incorrectly) set the tds version to 7.0. It should be 8.0 for freetds version 0.82 and 7.1 for freetds version 0.91. In this case 0.82 was installed.