Search⌘ K

Schemas and Tables

Explore how to design Ecto schemas that are not tied to database tables, enabling you to separate data representation from storage concerns and improve the user interface with flexible data models.

Downsides to locking schemas to tables

When we set up schemas for the first time, it is natural to add fields that exactly match our database tables. However, this can have some unintended side effects down the line. As we’ve seen, schemas become the backbone of our changesets, and changesets are what we use to parse user-submitted data and convey error messages about that data to the user.

For example, the phoenix_ecto package implements behaviors for the Phoenix web framework such that changesets can be used as the backing data structure for Phoenix forms. To take advantage of the conveniences provided by the Phoenix.Form module, our web forms need to match our changeset structure (and therefore the underlying schema) as much as possible.

Therefore, we turn our database ...