Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

399
Vistas
confusing error message on constraint error

Ecto is throwing me the following error:

** (exit) an exception was raised:
    ** (Ecto.ConstraintError) constraint error when attempting to insert struct:

    * unique: res_users_login_key

If you would like to convert this constraint into an error, please
call unique_constraint/3 in your changeset and define the proper
constraint name. The changeset defined the following constraints:

    * unique: res_users_login_index

Do I understand correctly that the actual name of the constraint in postgresql determines if the unique_constraint/3 function is successful or not? Just for reference, in postgreSQL the constraint is defined as follows:

Indexes:
    "res_users_pkey" PRIMARY KEY, btree (id)
    "res_users_login_key" UNIQUE CONSTRAINT, btree (login)

so _key and not _index.

I call the constraint function as follows:

 changeset |> unique_constraint(:login)

So, how do I make this work?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The constraint name used by Ecto when one is not given is #{table_name}_#{field}_index. Your table is probably named res_users, and the field is login, which is why Ecto uses the constraint name res_users_login_index, which is incorrect in your case. You'll need to explicitly specify the name res_users_login_key in the call to unique_constraint:

|> unique_constraint(:login, [name: :res_users_login_key])
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda