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

Return unique constraint errors for custom primary keys #7

Closed
wmartins opened this issue Nov 7, 2020 · 6 comments
Closed

Return unique constraint errors for custom primary keys #7

wmartins opened this issue Nov 7, 2020 · 6 comments

Comments

@wmartins
Copy link

wmartins commented Nov 7, 2020

Hi there! I've been using Etso for a while now and it's a great solution to get an Ecto based project up and running in no time.

One thing that I'm missing is to be able to get the error that happens when trying to insert a duplicate entry. In other words, I get a changeset that is invalid, however, the errors property is empty. The impact is that I'm unable to show this error information on a form.

I've been dabbling a litte with the implementation in order to check if it's possible, and here are my findings.

First, the empty error is justified by the following line:

if ets_result, do: {:ok, []}, else: {:invalid, []}

If I change that to this:

diff --git a/lib/etso/adapter/behaviour/schema.ex b/lib/etso/adapter/behaviour/schema.ex
index 6fe3992..fd8f7e1 100644
--- a/lib/etso/adapter/behaviour/schema.ex
+++ b/lib/etso/adapter/behaviour/schema.ex
@@ -21,7 +21,7 @@ defmodule Etso.Adapter.Behaviour.Schema do
     ets_field_names = TableStructure.field_names(schema)
     ets_changes = TableStructure.fields_to_tuple(ets_field_names, fields)
     ets_result = :ets.insert_new(ets_table, ets_changes)
-    if ets_result, do: {:ok, []}, else: {:invalid, []}
+    if ets_result, do: {:ok, []}, else: {:invalid, [unique: "primary_key"]}
   end
 
   def update(%{repo: repo}, %{schema: schema}, fields, filters, [], _) do

Now I start to receive the error:

  @primary_key{:key, :string, autogenerate: false}
  
  def changeset(schema, params \\ %{}) do
    schema
    |> unique_constraint(:key, name: :primary_key)
  end
    %{
      key: [
        {"has already been taken",
         [constraint: :unique, constraint_name: "primary_key"]}
      ]
    }

What are your thoughts on that, is that something you wish to support?

@evadne
Copy link
Owner

evadne commented Nov 29, 2020

Thanks. Apologies I have been busy recently. Will investigate this.

@evadne
Copy link
Owner

evadne commented Nov 29, 2020

Documentation for insert_new agrees with your proposed change. I shall raise a new release shorty. Thank you!!

@wmartins
Copy link
Author

You don't need to apologize @evadne :).

I'll be happy to test it when this new release is available. Thank you for the time to look into it!

@evadne
Copy link
Owner

evadne commented Mar 4, 2021

@wmartins FYI: Change is live @ feature/gh-7-unique-constraint-error. Will release new version when ready :>

@wmartins
Copy link
Author

wmartins commented Mar 4, 2021

Looking good @evadne, here's my application before and after those changes:

Before: not showing field validation error

Before: not showing field validation error

After: showing field validation error

After: showing field validation error

@evadne
Copy link
Owner

evadne commented Mar 6, 2021

Released 0.1.5 with thanks ❤️

@evadne evadne closed this as completed Mar 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants