Skip to content

Commit

Permalink
Enable identity insert on view's base table (#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan authored Sep 30, 2024
1 parent 8bd31c2 commit 9317467
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
#### Fixed

- [#1215](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1215) Fix mismatched foreign key errors
- [#1228](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1228) Enable identity insert on view's base table

Please check [7-1-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/7-1-stable/CHANGELOG.md) for previous changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def write_query?(sql) # :nodoc:

def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch:)
result = if id_insert_table_name = query_requires_identity_insert?(sql)
# If the table name is a view, we need to get the base table name for enabling identity insert.
id_insert_table_name = view_table_name(id_insert_table_name) if view_exists?(id_insert_table_name)

with_identity_insert_enabled(id_insert_table_name, raw_connection) do
internal_exec_sql_query(sql, raw_connection)
end
Expand Down
8 changes: 8 additions & 0 deletions test/cases/view_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ class ViewTestSQLServer < ActiveRecord::TestCase
assert_equal 1, klass.count
end
end

describe 'identity insert' do
it "identity insert works with views" do
assert_difference("SSTestCustomersView.count", 1) do
SSTestCustomersView.create!(id: 5, name: "Bob")
end
end
end
end

0 comments on commit 9317467

Please sign in to comment.