Skip to content

Support other unique constraint error format #115

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/ecto/adapters/sqlite3/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ defmodule Ecto.Adapters.SQLite3.Connection do
end

@impl true
def to_constraints(
%Exqlite.Error{message: "UNIQUE constraint failed: index " <> constraint},
_opts
) do
[unique: String.trim(constraint, ~s('))]
end

def to_constraints(
%Exqlite.Error{message: "UNIQUE constraint failed: " <> constraint},
_opts
Expand Down
34 changes: 34 additions & 0 deletions test/ecto/adapters/sqlite3/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,40 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
assert execute_ddl(integer) == [~s/CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY)/]
end

describe "to_constraints/2" do
alias Ecto.Adapters.SQLite3.Connection

test "unique index" do
# created with:
# CREATE UNIQUE INDEX users_email_name_index ON users (email);

error = %Exqlite.Error{message: "UNIQUE constraint failed: users.email"}
assert Connection.to_constraints(error, []) == [unique: "users_email_index"]
end

test "multi-column unique index" do
# created with:
# CREATE UNIQUE INDEX users_email_name_index ON users (email, name);

error = %Exqlite.Error{
message: "UNIQUE constraint failed: users.email, users.name"
}

assert Connection.to_constraints(error, []) == [unique: "users_email_name_index"]
end

test "complex unique index" do
# created with:
# CREATE UNIQUE INDEX users_email_year_index ON users (email, strftime('%Y', inserted_at));

error = %Exqlite.Error{
message: "UNIQUE constraint failed: index 'users_email_year_index'"
}

assert Connection.to_constraints(error, []) == [unique: "users_email_year_index"]
end
end

defp remove_newlines(string) do
string |> String.trim() |> String.replace("\n", " ")
end
Expand Down