You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The adapter uses `OUTPUT INSERTED` so that we can select any data type key, for example UUID tables. However, this poses a problem with tables that use triggers. The solution requires that we use a more complex insert statement which uses a temporary table to select the inserted identity. To use this format you must declare your table exempt from the simple output inserted style with the table name into a concurrent hash. Optionally, you can set the data type of the table's primary key to return.
```ruby
adapter = ActiveRecord::ConnectionAdapters::SQLServerAdapter
adapter.exclude_output_inserted_table_names['my_table_name'] = true
adapter.exclude_output_inserted_table_names['my_uuid_table_name'] = 'uniqueidentifier'
```
Copy file name to clipboardExpand all lines: README.md
+15-1Lines changed: 15 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,6 @@ The SQL Server adapter for ActiveRecord v5.1 using SQL Server 2012 or higher.
18
18
19
19
Interested in older versions? We follow a rational versioning policy that tracks Rails. That means that our 5.0.x version of the adapter is only for the latest 5.0 version of Rails. If you need the adapter for SQL Server 2008 or 2005, you are still in the right spot. Just install the latest 3.2.x to 4.1.x version of the adapter that matches your Rails version. We also have stable branches for each major/minor release of ActiveRecord.
20
20
21
-
22
21
#### Native Data Type Support
23
22
24
23
We support every data type supported by FreeTDS. All simplified Rails types in migrations will coorespond to a matching SQL Server national (unicode) data type. Always check the `initialize_native_database_types`[(here)](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/master/lib/active_record/connection_adapters/sqlserver/schema_statements.rb#L243) for an updated list.
@@ -28,6 +27,21 @@ The following types (date, datetime2, datetimeoffset, time) all require TDS vers
28
27
The Rails v5 adapter supports ActiveRecord's `datetime_with_precision` setting. This means that passing `:precision` to a datetime column is supported. Using a pecision with the `:datetime` type will signal the adapter to use the `datetime2` type under the hood.
29
28
30
29
30
+
#### Identity Inserts with Triggers
31
+
32
+
The adapter uses `OUTPUT INSERTED` so that we can select any data type key, for example UUID tables. However, this poses a problem with tables that use triggers. The solution requires that we use a more complex insert statement which uses a temporary table to select the inserted identity. To use this format you must declare your table exempt from the simple output inserted style with the table name into a concurrent hash. Optionally, you can set the data type of the table's primary key to return.
Although it is not necessary, the Ruby convention is to use lowercase method names. If your database schema is in upper or mixed case, we can force all table and column names during the schema reflection process to be lowercase. Add this to your config/initializers file for the adapter.
Copy file name to clipboardExpand all lines: test/schema/sqlserver_specific_schema.rb
+38Lines changed: 38 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -177,6 +177,44 @@
177
177
FROM sst_string_defaults
178
178
STRINGDEFAULTSBIGVIEW
179
179
180
+
# Trigger
181
+
182
+
execute"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sst_table_with_trigger') DROP TABLE sst_table_with_trigger"
183
+
execute"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sst_table_with_trigger_history') DROP TABLE sst_table_with_trigger_history"
184
+
execute<<-SQL
185
+
CREATE TABLE sst_table_with_trigger(
186
+
id bigint IDENTITY NOT NULL PRIMARY KEY,
187
+
event_name nvarchar(255)
188
+
)
189
+
CREATE TABLE sst_table_with_trigger_history(
190
+
id bigint IDENTITY NOT NULL PRIMARY KEY,
191
+
id_source nvarchar(36),
192
+
event_name nvarchar(255)
193
+
)
194
+
SQL
195
+
execute<<-SQL
196
+
CREATE TRIGGER sst_table_with_trigger_t ON sst_table_with_trigger
197
+
FOR INSERT
198
+
AS
199
+
INSERT INTO sst_table_with_trigger_history (id_source, event_name)
200
+
SELECT id AS id_source, event_name FROM INSERTED
201
+
SQL
202
+
203
+
execute"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sst_table_with_uuid_trigger') DROP TABLE sst_table_with_uuid_trigger"
204
+
execute<<-SQL
205
+
CREATE TABLE sst_table_with_uuid_trigger(
206
+
id uniqueidentifier DEFAULT NEWID() PRIMARY KEY,
207
+
event_name nvarchar(255)
208
+
)
209
+
SQL
210
+
execute<<-SQL
211
+
CREATE TRIGGER sst_table_with_uuid_trigger_t ON sst_table_with_uuid_trigger
212
+
FOR INSERT
213
+
AS
214
+
INSERT INTO sst_table_with_trigger_history (id_source, event_name)
0 commit comments