Hi,
I’m trying to give all users an unique token, but I got the following error when there is a duplicate:
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.
Post your migration code that’s there the constraint is created at.
Also you’re not doing anything wrong no? If you’re giving a duplicate the unique constraint should be yelling at you. Unless I’m not understanding what you’re trying to do.
Got this error, when i put a unique constraint on user_id
and html doesn’t have user_id as input.
i set it in controller => Users.create_user_preference(current_user, preference_params)
def create_user_preference(user, attrs \\ %{}) do
user
|> Ecto.build_assoc(:preference)
|> Preference.changeset(attrs)
|> Repo.insert()
end
def changeset(preference, attrs) do
preference
|> cast(attrs, [:age, :height, :religion_id, :height, :body_type,
:education_id, :occupation_id, :caste_id, :user_id])
|> validate_required([:religion_id, :user_id])
|> unique_constraint(:user_id, name: :preferences_user_id_index)
end
Getting the following error,
lists in Phoenix.HTML and templates may only contain integers representing bytes, binaries or other lists, got invalid entry: {:user_id, {“has already been taken”, [constraint: :unique, constraint_name: “preferences_user_id_index”]}}