我在Ruby on Rails应用程序中使用ActiveRecord gem。我执行以下命令将一条新记录保存到数据库:
Attachment.craete({IdAttachment: id , Name: name, Size: params[:file].size, UploadDate: DateTime.now })
由于我的表包含一个触发器,我得到这个错误:
ActiveRecord::StatementInvalid (TinyTds::Error:目标表DML语句的"附件"不能有任何启用的触发器,如果语句包含OUTPUT子句,不包含INTO子句):
检查activerecord自动生成的sql查询,查询中包含'OUTPUT inserts ',这是问题的原因。是否有任何方法可以避免在查询中包含"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
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'