dev-resources.site
for different kinds of informations.
Elixir Today: Remove duplicate rows in PostgreSQL using Ecto
Published at
4/6/2022
Categories
elixir
ecto
postgres
phoenix
Author
Pau Riosa
Intro
In this tutorial, I want to share with you how to remove duplicate rows in your schema using Ecto.
Preparation
- In your schema migration, let say you have this schema setup.
CREATE TABLE(:student_teacher, primary_key: false) do
add :id, :uuid, primary_key: true
add :student_id, references(:students, type: :uuid)
add :teacher_id, references(:teachers, type: :uuid)
end
- And then let's insert a couple of duplicate record. (Supposed you are using Ex.Machina for your data mock ups )
student = insert(:student)
teacher = insert(:teacher)
insert_list(100, :student_teacher, student: student, teacher: teacher)
Check for Duplicate Rows using IEx
Run iex -S mix
in your console and check for duplicate rows.
iex) query = SELECT COUNT(*), student_id, teacher_id FROM
student_teacher group by student_id, teacher_id having count(*) > 1;
iex) Ecto.Adapter.SQL.query!(Repo, query)
iex) %Postgrex.Result{
columns: ["count", "student_id", "teacher_id"],
command: :select
num_rows: 1,
rows: [
[
100,
<<student_id>>,
<<teacher_id>>
],
]
}
Run Query to delete Duplicate Rows
Repo.transaction(
fn ->
query = """
DELETE FROM student_teacher s1
USING student_teacher s2
where s1.id < s2.id
AND s1.student_id = s2.student_id
AND s1.teacher_id = s2.teacher_id
"""
Ecto.Adapters.SQL.query!(Repo, query)
end,
timeout: :infinity
)
Happy Coding!
Articles
12 articles in total
Thank you to all my followers! (and followers to be)
read article
How does Ecto.Schema's `has_one/3` works?
read article
Things I Learned using Phoenix LiveView in 2024 (Part 2)
read article
Things I Learned using Phoenix LiveView in 2024
read article
What is your win today?
read article
Elixir Today: Create a Left Triangle Pattern using Elixir
read article
Elixir Today: Create a Right Triangle Using Elixir
read article
Elixir Today: Create a Hollow Square using Elixir
read article
Elixir Today: Creating a Square Pattern using Elixir
read article
Elixir Today: Combining a List of Map via Specific Field
read article
Elixir Today: Remove duplicate rows in PostgreSQL using Ecto
currently reading
Elixir Regex: How to put an underscore between a letter and a number?
read article
Featured ones: