{"issue":{"pull_request":{"diff_url":null,"html_url":null,"patch_url":null},"state":"open","user":{"avatar_url":"https://secure.gravatar.com/avatar/c74a76a3bfbbc5f9ed041245c378de3b?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"c74a76a3bfbbc5f9ed041245c378de3b","url":"https://api.github.com/users/chadcf","login":"chadcf","id":23989},"milestone":null,"labels":[],"assignee":null,"updated_at":"2011-10-28T13:43:07Z","closed_at":null,"comments":16,"url":"https://api.github.com/repos/rails-sqlserver/activerecord-sqlserver-adapter/issues/139","body":"So I have a legacy db which has a rather poor design. In essence I have a users table, and an administrator table. If a user is an administrator, then their user id is inserted into the Administrator table. Which contains only one column, named ID, which is a primary key (but not an identity column).\r\n\r\nWhen I try to save a value to this table, the adapter tries to run \r\n\r\n    SET IDENTITY_INSERT [Administrator] ON\r\n\r\nThis fails with an error about not finding object Administrator and that perhaps I do not have permission to do this. So I talk to my DBA. He tells me that the ID column is not an identity column, therefore does not need this option set. I tell him that I realize this, but it's attempting to be set and if I could just get the permissions life would be swell. He then tells me that it since it's not an identity column, there is no permission to give. I don't quite buy this, but that's where I'm at.\r\n\r\nSo I look at the code and notice that the sqlserver adapter considers something an identity column if it is indeed an identity column, OR it is a primary key column. Hence my problem, as ID is a primary key column. This seems like an issue as it's entirely possible to have a column that is a primary key but not an identity column, and there's no need to make sure you can insert an identity to a non identity column. \r\n\r\nIs there a reason it does this? Is there any way to disable it other than to monkeypatch the adapter?","html_url":"https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/139","title":"IDENTITY_INSERT on primary key column","number":139,"id":2069589,"created_at":"2011-10-27T16:01:44Z"},"action":"created","comment":{"user":{"avatar_url":"https://secure.gravatar.com/avatar/9a45082056ded462905aa0382900a9e4?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"9a45082056ded462905aa0382900a9e4","url":"https://api.github.com/users/joncanady","login":"joncanady","id":6690},"updated_at":"2011-10-28T13:43:07Z","url":"https://api.github.com/repos/rails-sqlserver/activerecord-sqlserver-adapter/issues/comments/2556116","body":"Awesome Ken, thanks for the help. Let me know if you want a pull request for that failing test in the meantime. You're right, by the way: we know a fair bit about SQLServer (or, at least, our DBA does) but we're a little rusty on the internals of ActiveRecord. We rely on your adapter though, so getting a deeper understanding and contributing back is absolutely something we should be doing.\r\n\r\nI have a few other questions, just to perhaps shore up some of my confusion as I've dove into this:\r\n\r\n  > 4) Change our SQLServerColumn#initialize method to set @primary = @sqlserver_options[:is_identity] || @sqlserver_options[:is_primary]. Note that the @primary ivar is used in ActiveRecord to filter with the #content_columns method.\r\n\r\nI understand that @primary is used in #content_columns but why would you set @primary to a column that isn't the primary key? I tried to follow the thread for #60 but I'm afraid I still don't understand the reasoning behind assuming that PKs must be identities, or that identities must be PKs. I'm sure there's logic for it, I just don't see it.\r\n\r\nAlso: Just for kicks, I integrated the monkeypatch that @chadcf posted above and tried to re-run tests. The only two that failed were the two SQLServer tests around natural keys, complaining that you couldn't insert NULL into the PK columns. The PK is named \"legacy_id\", and in the setup you're never calling set_primary_key or similar. How were those tests passing before?\r\n\r\n","id":2556116,"created_at":"2011-10-28T13:43:07Z"}}