diff --git a/lib/sequel/adapters/shared/postgres.rb b/lib/sequel/adapters/shared/postgres.rb index cda8e1a94..5b128dbce 100644 --- a/lib/sequel/adapters/shared/postgres.rb +++ b/lib/sequel/adapters/shared/postgres.rb @@ -2430,6 +2430,9 @@ def insert_into_sql(sql) # Return the primary key to use for RETURNING in an INSERT statement def insert_pk (f = opts[:from]) && !f.empty? && (t = f.first) + + t = t.call(self) if t.is_a? Sequel::SQL::DelayedEvaluation + case t when Symbol, String, SQL::Identifier, SQL::QualifiedIdentifier if pk = db.primary_key(t) diff --git a/spec/adapters/postgres_spec.rb b/spec/adapters/postgres_spec.rb index 013819f26..e1c8519ee 100644 --- a/spec/adapters/postgres_spec.rb +++ b/spec/adapters/postgres_spec.rb @@ -2072,6 +2072,22 @@ def before_create end end +describe "Postgres::Dataset#import with delayed evaluation as source table" do + before do + @db = DB + @db.create_table!(:test){primary_key :x; Integer :y} + @ds = @db[Sequel::SQL::DelayedEvaluation.new(lambda{:test})] + end + after do + @db.drop_table?(:test) + end + + it "#import should work correctly when returning primary keys" do + @ds.import([:x, :y], [[1, 2], [3, 4]], :return=>:primary_key).must_equal [1, 3] + @ds.all.must_equal [{:x=>1, :y=>2}, {:x=>3, :y=>4}] + end +end + describe "Postgres::Dataset#insert" do before do @db = DB