2
고유 한 제약 조건이있는 값을 교환해야합니다. 아래에 표시된대로 update_all 함수를 사용하려고합니다.고유 한 구속 조건을 가진 값을 Ecto로 바꾸는 올바른 방법은 무엇입니까?
from(e in Episode, where: e.show_id == ^id, update: [set: [position: fragment("position + 1")]])
|> Repo.update_all([])
이를 사용하여 I 때문에 중복 위치의 오류를 얻을 :
ERROR (unique_violation): duplicate key value violates unique constraint "position_show_id_index"
table: episodes
constraint: position_show_id_index
Key ("position", show_id)=(2, 27) already exists.
어떻게 동시에 이러한 위치 값을 바꿀 수 있습니까?
나는 한 가지 방법은 트랜잭션을 사용하여 먼저 이전 레코드를 확인하고 삭제 한 다음 새 레코드를 삽입하는 것이라고 생각합니다. – JustMichael