Phoenix unique_constraint error while trying to build database
I am following along with Phoenix 1.4 book, and I got to "Using the Repository Data" section when I tried to put in data into through the terminal to the database I got an error.
My IEx session:
iex(1)> alias Rumbl.Repo
Rumbl.Repo
iex(2)> alias Rumbl.Accounts.User
Rumbl.Accounts.User
iex(3)> Repo.insert(%User{
...(3)> name: "José", username: "josevalim"
...(3)> })
[debug] QUERY ERROR db=16.7ms queue=1.2ms
INSERT INTO "users" ("name","username","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["José", "josevalim", ~N[2018-11-24 11:47:30], ~N[2018-11-24 11:47:30]]
It threw the following error:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* users_username_index (unique_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 `unique_constraint/3` on your changeset with the constraint
`:name` as an option.
The changeset has not defined any constraint.
(ecto) lib/ecto/repo/schema.ex:641: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
(elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
(ecto) lib/ecto/repo/schema.ex:626: Ecto.Repo.Schema.constraints_to_errors/3
(ecto) lib/ecto/repo/schema.ex:238: anonymous fn/15 in Ecto.Repo.Schema.do_insert/3
elixir phoenix-framework ecto
add a comment |
I am following along with Phoenix 1.4 book, and I got to "Using the Repository Data" section when I tried to put in data into through the terminal to the database I got an error.
My IEx session:
iex(1)> alias Rumbl.Repo
Rumbl.Repo
iex(2)> alias Rumbl.Accounts.User
Rumbl.Accounts.User
iex(3)> Repo.insert(%User{
...(3)> name: "José", username: "josevalim"
...(3)> })
[debug] QUERY ERROR db=16.7ms queue=1.2ms
INSERT INTO "users" ("name","username","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["José", "josevalim", ~N[2018-11-24 11:47:30], ~N[2018-11-24 11:47:30]]
It threw the following error:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* users_username_index (unique_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 `unique_constraint/3` on your changeset with the constraint
`:name` as an option.
The changeset has not defined any constraint.
(ecto) lib/ecto/repo/schema.ex:641: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
(elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
(ecto) lib/ecto/repo/schema.ex:626: Ecto.Repo.Schema.constraints_to_errors/3
(ecto) lib/ecto/repo/schema.ex:238: anonymous fn/15 in Ecto.Repo.Schema.do_insert/3
elixir phoenix-framework ecto
The error is indicating that you already have a row in the database with that name and username. What do you get when your runRepo.all(User)orRepo.get_by(User, name: "José", username: "josevalim")
– Steve Pallen
Nov 24 '18 at 13:54
add a comment |
I am following along with Phoenix 1.4 book, and I got to "Using the Repository Data" section when I tried to put in data into through the terminal to the database I got an error.
My IEx session:
iex(1)> alias Rumbl.Repo
Rumbl.Repo
iex(2)> alias Rumbl.Accounts.User
Rumbl.Accounts.User
iex(3)> Repo.insert(%User{
...(3)> name: "José", username: "josevalim"
...(3)> })
[debug] QUERY ERROR db=16.7ms queue=1.2ms
INSERT INTO "users" ("name","username","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["José", "josevalim", ~N[2018-11-24 11:47:30], ~N[2018-11-24 11:47:30]]
It threw the following error:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* users_username_index (unique_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 `unique_constraint/3` on your changeset with the constraint
`:name` as an option.
The changeset has not defined any constraint.
(ecto) lib/ecto/repo/schema.ex:641: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
(elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
(ecto) lib/ecto/repo/schema.ex:626: Ecto.Repo.Schema.constraints_to_errors/3
(ecto) lib/ecto/repo/schema.ex:238: anonymous fn/15 in Ecto.Repo.Schema.do_insert/3
elixir phoenix-framework ecto
I am following along with Phoenix 1.4 book, and I got to "Using the Repository Data" section when I tried to put in data into through the terminal to the database I got an error.
My IEx session:
iex(1)> alias Rumbl.Repo
Rumbl.Repo
iex(2)> alias Rumbl.Accounts.User
Rumbl.Accounts.User
iex(3)> Repo.insert(%User{
...(3)> name: "José", username: "josevalim"
...(3)> })
[debug] QUERY ERROR db=16.7ms queue=1.2ms
INSERT INTO "users" ("name","username","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["José", "josevalim", ~N[2018-11-24 11:47:30], ~N[2018-11-24 11:47:30]]
It threw the following error:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* users_username_index (unique_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 `unique_constraint/3` on your changeset with the constraint
`:name` as an option.
The changeset has not defined any constraint.
(ecto) lib/ecto/repo/schema.ex:641: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
(elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
(ecto) lib/ecto/repo/schema.ex:626: Ecto.Repo.Schema.constraints_to_errors/3
(ecto) lib/ecto/repo/schema.ex:238: anonymous fn/15 in Ecto.Repo.Schema.do_insert/3
elixir phoenix-framework ecto
elixir phoenix-framework ecto
edited Nov 25 '18 at 10:57
Sheharyar
44.4k10106160
44.4k10106160
asked Nov 24 '18 at 12:05
cggcgg
153
153
The error is indicating that you already have a row in the database with that name and username. What do you get when your runRepo.all(User)orRepo.get_by(User, name: "José", username: "josevalim")
– Steve Pallen
Nov 24 '18 at 13:54
add a comment |
The error is indicating that you already have a row in the database with that name and username. What do you get when your runRepo.all(User)orRepo.get_by(User, name: "José", username: "josevalim")
– Steve Pallen
Nov 24 '18 at 13:54
The error is indicating that you already have a row in the database with that name and username. What do you get when your run
Repo.all(User) or Repo.get_by(User, name: "José", username: "josevalim")– Steve Pallen
Nov 24 '18 at 13:54
The error is indicating that you already have a row in the database with that name and username. What do you get when your run
Repo.all(User) or Repo.get_by(User, name: "José", username: "josevalim")– Steve Pallen
Nov 24 '18 at 13:54
add a comment |
1 Answer
1
active
oldest
votes
It seems that you have created a unique index in your users table migration.
The constraint is on your username key meaning two records in the users table cannot have the same username. In your case, that means you have already created another user in the table with the username josevalim.
To get Ecto to return {:error, changeset} tuple instead of raising an error, you can tell the schema that there's a unique constraint in your changeset/2 method:
def changeset(user, params) do
user
|> cast(params, [:name, :username])
|> validate_required([:name, :username])
|> unique_constraint(:username, name: :users_username_index)
end
And use this method to build a changeset before inserting anything:
%User{}
|> changeset(%{jose: "José", username: "josevalim"})
|> Repo.insert
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457966%2fphoenix-unique-constraint-error-while-trying-to-build-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It seems that you have created a unique index in your users table migration.
The constraint is on your username key meaning two records in the users table cannot have the same username. In your case, that means you have already created another user in the table with the username josevalim.
To get Ecto to return {:error, changeset} tuple instead of raising an error, you can tell the schema that there's a unique constraint in your changeset/2 method:
def changeset(user, params) do
user
|> cast(params, [:name, :username])
|> validate_required([:name, :username])
|> unique_constraint(:username, name: :users_username_index)
end
And use this method to build a changeset before inserting anything:
%User{}
|> changeset(%{jose: "José", username: "josevalim"})
|> Repo.insert
add a comment |
It seems that you have created a unique index in your users table migration.
The constraint is on your username key meaning two records in the users table cannot have the same username. In your case, that means you have already created another user in the table with the username josevalim.
To get Ecto to return {:error, changeset} tuple instead of raising an error, you can tell the schema that there's a unique constraint in your changeset/2 method:
def changeset(user, params) do
user
|> cast(params, [:name, :username])
|> validate_required([:name, :username])
|> unique_constraint(:username, name: :users_username_index)
end
And use this method to build a changeset before inserting anything:
%User{}
|> changeset(%{jose: "José", username: "josevalim"})
|> Repo.insert
add a comment |
It seems that you have created a unique index in your users table migration.
The constraint is on your username key meaning two records in the users table cannot have the same username. In your case, that means you have already created another user in the table with the username josevalim.
To get Ecto to return {:error, changeset} tuple instead of raising an error, you can tell the schema that there's a unique constraint in your changeset/2 method:
def changeset(user, params) do
user
|> cast(params, [:name, :username])
|> validate_required([:name, :username])
|> unique_constraint(:username, name: :users_username_index)
end
And use this method to build a changeset before inserting anything:
%User{}
|> changeset(%{jose: "José", username: "josevalim"})
|> Repo.insert
It seems that you have created a unique index in your users table migration.
The constraint is on your username key meaning two records in the users table cannot have the same username. In your case, that means you have already created another user in the table with the username josevalim.
To get Ecto to return {:error, changeset} tuple instead of raising an error, you can tell the schema that there's a unique constraint in your changeset/2 method:
def changeset(user, params) do
user
|> cast(params, [:name, :username])
|> validate_required([:name, :username])
|> unique_constraint(:username, name: :users_username_index)
end
And use this method to build a changeset before inserting anything:
%User{}
|> changeset(%{jose: "José", username: "josevalim"})
|> Repo.insert
edited Nov 25 '18 at 10:57
answered Nov 25 '18 at 2:40
SheharyarSheharyar
44.4k10106160
44.4k10106160
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457966%2fphoenix-unique-constraint-error-while-trying-to-build-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The error is indicating that you already have a row in the database with that name and username. What do you get when your run
Repo.all(User)orRepo.get_by(User, name: "José", username: "josevalim")– Steve Pallen
Nov 24 '18 at 13:54