docjazither
Hi OGs,
I got a unique_constraint declared in my changeset as follow
defmodule App.Data.User do
use Ecto.schema
import Ecto.Changeset
schema "users" do
field(:full_name, :string)
field(:email, :string)
field(:username, :string)
field(:password, :string)
end
@required_fields []
@optional_fields[
:fullname,
:email,
:username,
:password]
def changeset(struct, params \\ :empty) do
struct
|> cast(params, @required_fields ++ @optional_fields)
|> StringValidator.validate()
|>validate_required(@required_fields)
|> unique_constraint(:email)
end
My migration:
defmodule App.Data.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users) do
add(:full_name, :string)
add(:email, :string)
end
create(unique_index(:users, :email))
create(index(:users, :full_name))
end
end
My controller :
def create(attrs \\ %{}, status_code \\ "approached", opts \\ [sync: true]) do
with {:ok, user} <-
status_code
|> user_with_status()
|> User.changeset(attrs)
|> Repo.insert(),
insert_or_update_subscriber(user, opts)
{:ok, user}
end
end
But I still can create a new user with the same email, that would make a duplicate entry
Any thoughts ?
Thanks
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
joseph-lozano
@required_fields []You aren’t passing
:emailintocast, so you are creating entries in your database that don’t have an email at all, which is why theunique_constraintis not triggering.malloryerik
I’ve also forgotten to simply pass fields into
castso many times…docjazither
Hi @joseph-lozano
Thanks for the reply, I tried and it still doesn’t work,
I passed it into
castand re-ranecto.migrationsas well.f0rest8
Just for clarity, is it not working in that you’re expecting to see an error returned in the changeset and not be able to submit the form? Or you’ve checked the actual database and seen that duplicate emails are being saved?
I’m not sure if I’m asking clearly, but I ask because
unsafe_validate_unique/4can be used to quickly return an invalid changeset error for UI purposes, and then you follow it up with a call tounique_constraint/3to ensure the constraint at the database level.Another thing to check, is that you are calling
Ecto.Repo.insert/2orEcto.Repo.update/2in yourUsers.createfunction from yourUsers.create(user_params)call.docjazither
Hi @f0rest8 ,
Yes, I checked the postgres database and I’ve seen that duplicate emails are saved there.
I also tried your suggestion that to call
Ecto.Repo.Insert/2, and it did insert it with the same email.joseph-lozano
It might be that you added the unique_index to the migration after they had already run. Have you ensured that you have rolled-back and then re-run the migrations?
csadewa
Second this, it seems the problem is actually unique index is not created on DB when it should be created. That’s why DB allow duplicate email. For me, this occasionally happen when i was accidentally run ecto migration even when i haven’t finish written migration file (solution: rollback and migrate, though sometime you need to adjust the migration file to previous version first before rollback), or i just forgot to run the migration
03juan
You’ll get a postgres
unique_violationerror when trying to add a unique index on a column that already contains duplicated values, and will even tell you which value it failed on:03juan
Are you sure it’s allowing duplicates when testing with non-NULL emails? Because according to psql docs PostgreSQL: Documentation: 18: 5.5. Constraints
dimitarvp
As a starting point, do try to insert two records with identical emails from inside
psqland see where that gets you.If you can’t, make sure you have undone the migration that adds the index and then re-run it.