Making usernames unique

At the moment, multiple people can register with the same username, something that would inevitably cause a lot of problems in a real-world situation. Ecto does not allow us to verify the username's uniqueness without there being a database-level constraint check, so let's start by creating a migration to make usernames unique in the database. First, let's generate the migration:

$ mix ecto.gen.migration make_usernames_unique* creating priv/repo/migrations* creating priv/repo/migrations/20180201223131_make_usernames_unique.exs

Then, we'll give the migration the following code. Note that all we're doing here is telling Ecto to add a unique index to the users table on the username column:

defmodule Vocial.Repo.Migrations.MakeUsernamesUnique ...

Get Phoenix Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.