rails-sqlserver / activerecord-sqlserver-adapter

SQL Server Adapter For Rails
MIT License
972 stars 558 forks source link

Creating fixtures for models based on a view containing an IDENTITY column #1224

Open budiljak opened 3 days ago

budiljak commented 3 days ago

Fixtures for Rails tests cannot be created if an ActiveRecord model is created on a view instead of a table and the tables primary key is an IDENTITY column. While creating fixtures for such a model the rails test environment needs to turn on IDENTITY_INSERT. This fails for views as it has to be executed on the underlying table.

The error message is: IDENTITY_INSERT could not be turned OFF for table [xyz]

([xyz] would be the name of the view).

Expected behavior

Should call "SET IDENTITY_INSERT" on the underlying table.

Actual behavior

Calls "SET IDENTITY_INSERT" on the view the model is based on.

Solution

See def set_identity_insert(table_name, conn, enable) (lib/active_record/connection_adapters/sqlserver/database_statements.rb line 303)

table_name is view name here.

aidanharan commented 2 days ago

@budiljak Could you provide a test case (https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/wiki/How-to-report-a-bug) to demonstrate the issue?

budiljak commented 2 days ago

Hi @aidanharan. As the bug is pretty obvious I thought you won't need the test case. But I understand how tiring it is to chase bugs that aren't bugs. Here's the testcase.

testcase.zip

Cheers, Ben