简体   繁体   中英

How to avoid includeing OUTPUT INSERTED when adding a new record to database

I am using the ActiveRecord gem in a Ruby on Rails application. I execute this command to save a new record to my database:

Attachment.craete({IdAttachment: id , Name: name, Size: params[:file].size, UploadDate: DateTime.now })

Since my table contains a trigger, I am getting this error:

ActiveRecord::StatementInvalid (TinyTds::Error: The target table 'Attachment' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.):

Checking the auto generated sql query by activerecord, the query contains 'OUTPUT INSERTED' which is the cause of the issue. Is there any way we can avoid including 'OUTPUT INSERTED' into the query?

EXEC sp_executesql N'INSERT INTO [Attachment] 
([IdAttachment], [Title], [IdVch], [IdForm], [Name], [Size], [UploadDate], [IdUser])
OUTPUT INSERTED.[IdAttachment] 
VALUES (@0, @1, @2, @3, @4, @5, @6, @7)', 
N'@0 int, @1 nvarchar(max), @2 int, @3 int, @4 nvarchar(max), @5 float, @6 datetime, @7 int', @0 = 21007, @1 = NULL, @2 = NULL, @3 = NULL, @4 = N'uf21007_pic.jpg', @5 = 87041.0, @6 = '11-20-2022 13:23:20.706', @7 = NULL

from https://github.com/rails-sqlserver/activerecord-sqlserver-adapter#identity-inserts-with-triggers :

  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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM