Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with PK ID field returned with ActiveRecord - Sqlserver - TinyTDS #19

Closed
ejlevin1 opened this issue Apr 28, 2011 · 15 comments
Closed

Comments

@ejlevin1
Copy link

I'm running into some issues with the incorrect PK being returned
to active record upon a model object being created. I've debugged
things quite a bit, and the SQL is getting into TinyTDS, the wrong ID
gets returned? For some reason the ID always gets returned as 19...
Its maddening :) Any help or thoughts on what is going on, would be
much appreciated! Thanks!
OK, so here are some of the details:
Debugged ActiveRecord::Persistence.create
and saw that the value of new_id was being returned as 19.
------------- SNIPPET -------------
new_id = if attributes_values.empty?
self.class.unscoped.insert
connection.empty_insert_statement_value
else
self.class.unscoped.insert attributes_values
end

self.id ||= new_id

Here is all I'm doing:
user = User.new :email => 'em...@test.com'
user.authentications.build( :provider => 'provider_name', :uid =>
self.employee_id )
user.first_name = 'Eric'
user.save!
The problem is that the new user gets assigned the value of 19 as the
PK ID (column name is ID), then the authentications child gets the
wrong FK assigned during its creation.
tSQL Config:
titanium:gems ejlevin1$ tsql -C
Compile-time settings (established with the "configure" script)
Version: freetds v0.82
freetds.conf directory: /opt/local/etc/freetds
MS db-lib source compatibility: no
Sybase binary compatibility: no
Thread safety: yes
iconv library: yes
TDS version: 5.0
iODBC: no
unixodbc: yes
freetds.conf Config section:
[myQA]
host = HOSTNAME
port = 1433
tds version = 8.0

@metaskills
Copy link
Member

What version of SQL Server are you on? Also, have you made sure you do not have a TDSVER environment variable http://www.freetds.org/userguide/envvar.htm set anywhere that would be trumping the tds version in the conf file do you?

@ejlevin1
Copy link
Author

SQL Server 2005. I looked at the [global] section of the config, but made sure that was set to 8.0 as well....

titanium:~ ejlevin1$ echo $TDSVER

titanium:~ ejlevin1$

@metaskills
Copy link
Member

I have an idea what the problem could be. Will report back tomorrow.

@metaskills
Copy link
Member

I created a patch on a hunch. See the commit 93ca696 for details. Basically my assumption was that there was some C math problems when a large init or bigint was returned for a primary key. So I made the ruby functions that cast that returned scope to follow the same logic we use when casting big inits, which that identity sql is doing too.

I just pushed 0.4.5.rc3 gems with these changes. Can you please test them and let me know?

@metaskills
Copy link
Member

Closing this issue since I never heard back before 0.4.5 release.

@ejlevin1
Copy link
Author

ejlevin1 commented Jun 3, 2011

Ken -- really sorry about not getting back to you faster on this. The email notification for this was not going to my correct email address so I was out of the loop on follow ups. I just tested with the 0.4.5 tiny-tds gem and I am still seeing the issue. I am running activerecord-sqlserver-adapter gem v3.0.10. The IDs are relatively small right now (my test was on ID 606). Thoughts?

@metaskills
Copy link
Member

I'm reopening this. Let's hammer out what your issue is. The good news is if this was a big problem, I would be hearing more about it. So we can assume this might be something with just your setup. So let's take that into account when going back and forth. I see you posted something to the adapter list too. Lemme go take a look.

@metaskills metaskills reopened this Jun 3, 2011
@metaskills
Copy link
Member

Can you try updating to the latest FreeTDS, I wrote a macports file here.
https://github.com/metaskills/macports

Also, you can clone the TinyTDS repo and just run build your own version of TinyTDS bundled with freetds & libiconv. Follow the directions in the "Using MiniPortile" section of the README. Basically this will download the latest FreeTDS for you and build a gem for your platform so you do not have to worry about what is installed on your system (in your case in opt directory).

@ejlevin1
Copy link
Author

Unfortunately I am using homebrew on my machine and couldn't easily make the transition from the portfile you have. I'll take a look at this later tonight and add MacPorts back onto my machine to test it out. Stand by...

@metaskills
Copy link
Member

No no no... do the second thing I said. TinyTDS comes with it's own little port system called MiniPortile. Read up on it in the README. It will download and compile locally to the project.

@ejlevin1
Copy link
Author

Ken -- ok so I cloned the tiny_tds repo, bundled/compiled/etc in an rvm gemset (uniquely named 1.8.7@tiny_tds). It successfully pulled in the mini_protfile gem and built the native tiny_tds gem for my system within the pkg folder of the project:

drwxr-xr-x  13 ejlevin1  staff    442 Jun 15 08:28 tiny_tds-0.4.5
-rw-r--r--   1 ejlevin1  staff  46592 Jun 15 08:28 tiny_tds-0.4.5-x86-darwin-10.gem
-rw-r--r--   1 ejlevin1  staff  35328 Jun 15 08:28 tiny_tds-0.4.5.gem

My question then is how do I install this native gem into my other RVM repo (1.8.7@another_repo). I was looking at the --local flag for ruby gems, but wasn't sure whether to specify a special name for the native gem, or just

gem install -l tiny_tds-0.4.5

If you could possibly give me some guidance on this last step I'd really appreciate it! Thanks!

@metaskills
Copy link
Member

Sure, can you hop onto #rails-sqlserver in IRC? I am there now, but have to do a few errands. Just hang around and I'll show up.

@metaskills
Copy link
Member

Where did we leave off on this?

@ejlevin1
Copy link
Author

Hey Jeff -- this was resolved when I built Freetds with MiniPort. Sorry I forgot to close it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants