Skip to content

Commit c3968ba

Browse files
committed
Enable identity insert on view's base table
1 parent c80d620 commit c3968ba

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@
99
- [#1153](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1153) Only support Ruby v3.1+
1010
- [#1196](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1196) Use default inspect for database adapter
1111

12+
#### Fixed
13+
14+
- [#](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/) Enable identity insert on view's base table
15+
1216
Please check [7-1-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/7-1-stable/CHANGELOG.md) for previous changes.

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: fa
4242
log(sql, name, binds, async: async) do |notification_payload|
4343
with_raw_connection do |conn|
4444
result = if id_insert_table_name = query_requires_identity_insert?(sql)
45+
# If the table name is a view, we need to get the base table name for enabling identity insert.
46+
id_insert_table_name = view_table_name(id_insert_table_name) if view_exists?(id_insert_table_name)
47+
4548
with_identity_insert_enabled(id_insert_table_name, conn) do
4649
internal_exec_sql_query(sql, conn)
4750
end

test/cases/view_test_sqlserver.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ class ViewTestSQLServer < ActiveRecord::TestCase
4747
assert_equal 1, klass.count
4848
end
4949
end
50+
51+
describe 'identity insert' do
52+
it "identity insert works with views" do
53+
assert_difference("SSTestCustomersView.count", 1) do
54+
SSTestCustomersView.create!(id: 5, name: "Bob")
55+
end
56+
end
57+
end
5058
end

0 commit comments

Comments
 (0)