在单个事务中插入多个变更集

inserting multiple changesets in single transaction

我有这座桥 table :

schema "rooms_units" do
  field(:date_from, :utc_datetime)
  field(:date_to, :utc_datetime)
  belongs_to(:room, App.Room, primary_key: true)
  belongs_to(:unit, App..Unit)
end

我有来自我的端点的地图列表,我为每个地图制作了一个变更集列表。

[

#Ecto.Changeset<
 action: nil,
changes: %{
  date_from: #DateTime<2016-11-03 13:23:00Z>,
  date_to: #DateTime<2016-11-03 13:23:00Z>,
  room_id: 255,
  unit_id: 296
},
errors: [],
data: #App.RoomUnit<>,
valid?: true

#Ecto.Changeset<
action: nil,
changes: %{
  date_from: #DateTime<2016-11-03 13:23:00Z>,
  date_to: #DateTime<2016-11-03 13:23:00Z>,
  room_id: 256,
  unit_id: 296
},
errors: [],
data: #App.RoomUnit<>,
valid?: true
>
]

我想在单笔交易中将其插入 rooms_units table。

我试过了Ecto.multi.insert_all。但它接受地图列表而不是变更集。还有其他功能可以帮助我吗

谢谢

使用MyRepo.transaction/2.

MyRepo.transaction(fn ->
  Enum.each(changesets, &MyRepo.update!(&1, []))
end)