activerecord-sqlserver-adapter icon indicating copy to clipboard operation
activerecord-sqlserver-adapter copied to clipboard

Problem trying to initialize with adapter = ActiveRecord::ConnectionAdapters::SQLServerAdapter

Open apcano1978 opened this issue 7 years ago • 2 comments

Hello, I have a problem trying to insert to a remote table with a trigger where I get the following error:

SQL (23.7ms)  EXEC sp_executesql N'INSERT INTO [apianaconda_agents] ([anaconda_id], [partner_id], [partner], [office_id], [office_text], [reference], [name], [email], [phone], [mobile], [fax], [vat], [picture], [education], [position], [enabled], [anaconda_updated_at], [created_at], [updated_at], [geo_city_id]) OUTPUT INSERTED.[id] VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19)', N'@0 int, @1 int, @2 nvarchar(4000), @3 int, @4 nvarchar(4000), @5 nvarchar(4000), @6 nvarchar(4000), @7 nvarchar(4000), @8 nvarchar(4000), @9 nvarchar(4000), @10 nvarchar(4000), @11 nvarchar(4000), @12 nvarchar(4000), @13 nvarchar(4000), @14 nvarchar(4000), @15 bit, @16 datetime, @17 datetime, @18 datetime, @19 int', @0 = 3456, @1 = 2, @2 = N'REMAX', @3 = 733, @4 = N'RE/MAX Luna', @5 = N'35081788', @6 = N'Isabel Gonzalez Guerrero', @7 = N'[email protected]', @8 = N'', @9 = N'', @10 = N'', @11 = N'', @12 = N'', @13 = N'', @14 = N'', @15 = 1, @16 = '02-26-2018 11:43:00.0', @17 = '02-01-2019 10:43:31.253', @18 = '02-01-2019 10:43:31.253', @19 = 28001  [["anaconda_id", nil], ["partner_id", nil], ["partner", nil], ["office_id", nil], ["office_text", nil], ["reference", nil], ["name", nil], ["email", nil], ["phone", nil], ["mobile", nil], ["fax", nil], ["vat", nil], ["picture", nil], ["education", nil], ["position", nil], ["enabled", nil], ["anaconda_updated_at", nil], ["created_at", nil], ["updated_at", nil], ["geo_city_id", nil]]
  SQL (6.5ms)  IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
{ 70137930500520 rufus-scheduler intercepted an error:
  70137930500520   job:
  70137930500520     Rufus::Scheduler::InJob "1s" {:no_error=>true, :_t=>1.0}
  70137930500520   error:
  70137930500520     70137930500520
  70137930500520     ActiveRecord::StatementInvalid
  70137930500520     TinyTds::Error: The target table 'apianaconda_agents' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.: EXEC sp_executesql N'INSERT INTO [apianaconda_agents] ([anaconda_id], [partner_id], [partner], [office_id], [office_text], [reference], [name], [email], [phone], [mobile], [fax], [vat], [picture], [education], [position], [enabled], [anaconda_updated_at], [created_at], [updated_at], [geo_city_id]) OUTPUT INSERTED.[id] VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19)', N'@0 int, @1 int, @2 nvarchar(4000), @3 int, @4 nvarchar(4000), @5 nvarchar(4000), @6 nvarchar(4000), @7 nvarchar(4000), @8 nvarchar(4000), @9 nvarchar(4000), @10 nvarchar(4000), @11 nvarchar(4000), @12 nvarchar(4000), @13 nvarchar(4000), @14 nvarchar(4000), @15 bit, @16 datetime, @17 datetime, @18 datetime, @19 int', @0 = 3456, @1 = 2, @2 = N'REMAX', @3 = 733, @4 = N'RE/MAX Luna', @5 = N'35081788', @6 = N'Isabel Gonzalez Guerrero', @7 = N'[email protected]', @8 = N'', @9 = N'', @10 = N'', @11 = N'', @12 = N'', @13 = N'', @14 = N'', @15 = 1, @16 = '02-26-2018 11:43:00.0', @17 = '02-01-2019 10:43:31.253', @18 = '02-01-2019 10:43:31.253', @19 = 28001
failure in #on_error itself:
#<NoMethodError: undefined method `each' for nil:NilClass> 

Then, I checked your documentation and, as you mention:

The adapter uses OUTPUT INSERTED so that we can select any data type key, for example UUID tables. However, this poses a problem with tables that use triggers. The solution requires that we use a more complex insert statement which uses a temporary table to select the inserted identity. To use this format you must declare your table exempt from the simple output inserted style with the table name into a concurrent hash. Optionally, you can set the data type of the table's primary key to return.

adapter = ActiveRecord::ConnectionAdapters::SQLServerAdapter

Will assume `bigint` as the id key temp table type.
adapter.exclude_output_inserted_table_names['my_table_name'] = true

Explicitly set the data type for the temporary key table.
adapter.exclude_output_inserted_table_names['my_uuid_table_name'] = 'uniqueidentifier'

Well, I have created an initializer called activerecord-sqlserver-adapter.rb with the following content

adapter = ActiveRecord::ConnectionAdapters::SQLServerAdapter

adapter.exclude_output_inserted_table_names['apianaconda_agents'] = true 

But now, when I try to start my rails server, then I get the following error and I cannot start the application:

 /Users/apcano1978/projects/babel/babel_modules/apianaconda/config/initializers/activerecord-sqlserver-adapter.rb:3:in `<top (required)>': uninitialized constant ActiveRecord::ConnectionAdapters::SQLServerAdapter (NameError) 

It looks like the adapter is not loaded or something, I don´t really know what might be wrong because the adapter is working (except for the insert on that table)

There is something that I think it should not affect, but maybe it is good information for you, the adapter is inside an engine, not in the main app, but the initializer is also inside the engine config folder.

Well, I hope you can give me some support cause right now I´m quite stuck at this point and I don´t know what else can I try.

Best regards

Antonio

apcano1978 avatar Feb 01 '19 10:02 apcano1978

Hi @apcano1978. Would you be able to create a tiny project and host it somewhere so we can reproduce the issue? I would like to see your main app and engine Gemfiles.

wpolicarpo avatar Feb 01 '19 11:02 wpolicarpo

I'm closing this off as it seems an issue with the way the application is configured rather than the adapter itself.

wpolicarpo avatar Mar 27 '20 10:03 wpolicarpo