簡體   English   中英

向數據庫添加新記錄時如何避免包含 OUTPUT INSERTED

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

我在 Ruby on Rails 應用程序中使用 ActiveRecord gem。 我執行此命令以將新記錄保存到我的數據庫中:

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

由於我的表包含觸發器,因此出現此錯誤:

ActiveRecord::StatementInvalid(TinyTds::Error:如果語句包含沒有 INTO 子句的 OUTPUT 子句,則 DML 語句的目標表“附件”不能有任何啟用的觸發器。):

檢查 activerecord 自動生成的 sql 查詢,查詢包含“OUTPUT INSERTED”,這是問題的原因。 有什么辦法可以避免在查詢中包含“OUTPUT INSERTED”嗎?

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

來自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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM