Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ecto.Changeset check_constraint function not working as expected #4415

Open
Artexis10 opened this issue May 16, 2024 · 4 comments
Open

Ecto.Changeset check_constraint function not working as expected #4415

Artexis10 opened this issue May 16, 2024 · 4 comments

Comments

@Artexis10
Copy link

Artexis10 commented May 16, 2024

Elixir version

1.15.6

Database and Version

PostgreSQL 11.12

Ecto Versions

3.10.3

Database Adapter and Versions (postgrex, myxql, etc)

0.17.3

Current behavior

Here is the implementation:

Migration:

    create constraint("notes", :entity_id_entity_code,
             check: "entity_id IS NOT NULL OR entity_code IS NOT NULL",
             prefix: "public"
           )

Schema:
check_constraint(:entity_id, name: :entity_id_entity_code)

Following the documentation of check_constraint, this is the error that occurs:

** (Ecto.ConstraintError) constraint error when attempting to insert struct:

         * "entity_id_entity_code" (check_constraint)

     If you would like to stop this constraint violation from raising an
     exception and instead add it as an error to your changeset, please
     call `check_constraint/3` on your changeset with the constraint
     `:name` as an option.

     The changeset has not defined any constraint.

Stacktrace:

stacktrace:
       (ecto 3.10.3) lib/ecto/repo/schema.ex:804: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
       (elixir 1.15.6) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
       (ecto 3.10.3) lib/ecto/repo/schema.ex:788: Ecto.Repo.Schema.constraints_to_errors/3
       (ecto 3.10.3) lib/ecto/repo/schema.ex:769: Ecto.Repo.Schema.apply/4
       (ecto 3.10.3) lib/ecto/repo/schema.ex:377: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
       (ecto 3.10.3) lib/ecto/repo/schema.ex:273: Ecto.Repo.Schema.insert!/4

Expected behavior

The expectation is that the Ecto.ConstraintError is handled and does not raise an exception. Currently, it does not and suggests that to handle this, the check_constraint function needs to be defined on the changeset, which it already is.

@josevalim
Copy link
Member

Can you please provide the full code?

The fact the error message does not list your constraint seems to indicate that the constraint is not effectively stored in the changeset. Maybe you are setting the constraint and then discarding the changeset (or returning another changeset).

@Artexis10
Copy link
Author

I am unable to provide more code as there would be a confidentiality issue. In the code, I am returning the same changeset, so it is unlikely to be that.

@Artexis10
Copy link
Author

Are you able to replicate the issue?

@ericmj
Copy link
Member

ericmj commented May 16, 2024

You need to do provide us a way to replicate the issue. We have tests for check_constraint that pass so it's not easy for us to know how to replicate your issue.

Can you write some example code that shows the issue, ideally by providing a mix project that reproduces the error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants