Model.save does not respect Model.dataset.disable_insert_output #519

Closed
munkyboy opened this Issue Jul 16, 2012 · 3 comments

Projects

None yet

2 participants

@munkyboy

In MSSQL, if you have a view with an insert trigger defined, you can not

INSERT INTO [view] (...) OUTPUT [inserted].* VALUES (...)

I've been utilizing the MSSQL adapter method disable_insert_output. In 3.34.0 (specifically this commit) this spec will fail.

describe "Model#create" do
  context "on a view" do
    let(:model) { Sequel::Model(:items) }

    before do
      MSSQL_DB.create_table!(:items_clean) { String :name }
      MSSQL_DB.create_view(:items, MSSQL_DB[:items_clean])
      MSSQL_DB.execute(<<-SQL)
        create trigger items_insert
        on items
        instead of insert
        as begin
          INSERT INTO items_clean (name) SELECT name FROM inserted
        end
      SQL
      model.dataset.disable_insert_output!
    end

    after do
      MSSQL_DB.drop_view(:items)
      MSSQL_DB.drop_table(:items_clean)
    end

    specify "works" do
      expect { model.create(:name => 'a') }.to_not raise_error
      model.count.should == 1
    end
  end
end

You introduced a new method called instance_dataset. What's your recommendation for fixing this? Should I use

MyModel.instance_dataset.disable_insert_output!

wherever I was previously using

MyModel.dataset.disable_insert_output!
@jeremyevans
Owner

You should never call a mutation method on a model dataset. Do MyModel.dataset = MyModel.dataset.disable_insert_output

@munkyboy

fair enough. However Model#save will eventually use the instance_dataset which is initialized via:

@instance_dataset = @dataset.limit(1).naked

which doesn't copy over the base dataset options.

@munkyboy

nevermind. I see that the setter sets the instance_dataset and opts are preserved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment