Skip to Content
Phoenix Web Development
book

Phoenix Web Development

by Brandon Richey
April 2018
Beginner to intermediate content levelBeginner to intermediate
406 pages
9h 33m
English
Packt Publishing
Content preview from Phoenix Web Development

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Phoenix in Action

Phoenix in Action

Geoffrey Lessel
Real-Time Phoenix

Real-Time Phoenix

Stephen Bussey

Publisher Resources

ISBN: 9781787284197Supplemental Content