@@ -2123,32 +2123,8 @@ defmodule AshPostgres.DataLayer do
21232123                { Map . take ( r ,  keys ) ,  r } 
21242124              end ) 
21252125
2126-             cant_map_results_to_changesets  = 
2127-               any_generated_keys_missing? ( keys ,  resource ,  changesets ) 
2128- 
21292126            results  = 
2130-               if  cant_map_results_to_changesets  do 
2131-                 results 
2132-                 |>  Enum . zip ( changesets ) 
2133-                 |>  Enum . map ( fn  { result ,  changeset }  -> 
2134-                   if  ! opts [ :upsert? ]  do 
2135-                     maybe_create_tenant! ( resource ,  result ) 
2136-                   end 
2137- 
2138-                   case  get_bulk_operation_metadata ( changeset )  do 
2139-                     { index ,  metadata_key }  -> 
2140-                       Ash.Resource . put_metadata ( result ,  metadata_key ,  index ) 
2141- 
2142-                     nil  -> 
2143-                       # Compatibility fallback 
2144-                       Ash.Resource . put_metadata ( 
2145-                         result , 
2146-                         :bulk_create_index , 
2147-                         changeset . context [ :bulk_create ] [ :index ] 
2148-                       ) 
2149-                   end 
2150-                 end ) 
2151-               else 
2127+               if  opts [ :upsert? ]  do 
21522128                changesets 
21532129                |>  Enum . map ( fn  changeset  -> 
21542130                  identity  = 
@@ -2176,9 +2152,28 @@ defmodule AshPostgres.DataLayer do
21762152                    end 
21772153                  end 
21782154                end ) 
2179-                 |>  Enum . concat ( results ) 
21802155                |>  Enum . filter ( &  & 1 ) 
2181-                 |>  Enum . uniq_by ( & Map . take ( & 1 ,  keys ) ) 
2156+               else 
2157+                 results 
2158+                 |>  Enum . zip ( changesets ) 
2159+                 |>  Enum . map ( fn  { result ,  changeset }  -> 
2160+                   if  ! opts [ :upsert? ]  do 
2161+                     maybe_create_tenant! ( resource ,  result ) 
2162+                   end 
2163+ 
2164+                   case  get_bulk_operation_metadata ( changeset )  do 
2165+                     { index ,  metadata_key }  -> 
2166+                       Ash.Resource . put_metadata ( result ,  metadata_key ,  index ) 
2167+ 
2168+                     nil  -> 
2169+                       # Compatibility fallback 
2170+                       Ash.Resource . put_metadata ( 
2171+                         result , 
2172+                         :bulk_create_index , 
2173+                         changeset . context [ :bulk_create ] [ :index ] 
2174+                       ) 
2175+                   end 
2176+                 end ) 
21822177              end 
21832178
21842179            { :ok ,  results } 
@@ -3765,16 +3760,6 @@ defmodule AshPostgres.DataLayer do
37653760    end 
37663761  end 
37673762
3768-   # checks if any of the attributes in the list of keys are generated and missing from any of the changesets 
3769-   # if so, we can't match the created record to the changeset by the identity and just need to zip the return 
3770-   # values with the changesets 
3771-   defp  any_generated_keys_missing? ( keys ,  resource ,  changesets )  do 
3772-     Enum . any? ( keys ,  fn  key  -> 
3773-       Ash.Resource.Info . attribute ( resource ,  key ) . generated?  && 
3774-         Enum . any? ( changesets ,  fn  changeset  ->  is_nil ( changeset . attributes [ key ] )  end ) 
3775-     end ) 
3776-   end 
3777- 
37783763  defp  get_bulk_operation_metadata ( changeset )  do 
37793764    changeset . context 
37803765    |>  Enum . find_value ( fn 
0 commit comments