Return to site

Ecto 3 0 2 – Post Entries To Weblog Systems

broken image


Edgar Pino

October 12, 2018

Note: This post was updated to work with Ecto 3.0

Welcome to part two of Getting Started with Ecto. On the last post, we covered how to install and configure Ecto in our application. In this post we will cover migrations, schemas, and changesets in Ecto.

  1. Migrations, Schemas, and Changesets (This Post)

Migrations

Now that we have a database and Repo, we are going to create tables and columns. To do that, we will create migrations that define the structure of our tables and define any relationships. Before we create our first migration, let's look at the database design of our demo application.

Oct 3, 2020 Publish documentation for release 3.5.0 Oct 3, 2020 Publish release 3.5.0 Sep 19, 2020 Publish documentation for release 3.5.0-rc.1. Ecto is a feature-rich desktop blogging client for MacOSX and Windows, supporting a wide range of weblog systems, such as Blogger, Blojsom, Drupal, MovableType, Nucleus, TypePad, WordPress, and more. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

  • Users have credentials with a unique email
  • Challenges have many solutions
  • Users can have one solution per challenge

Generating Migrations

Let's go ahead and create our first migration by running the following generator command:

That will create a file inside of priv/repo/migrations/ that looks something like this:

Lets define our table inside the change function; this allows us to create reversible migrations.

The column name and type are defined as atoms. We defined the name as a string with a character limit of 100 and age is an integer. We also use the timestamps function that adds :inserted_at and :updated_at timestamps columns. See the docs for other primitive types.

Running Migrations

Now that we have our migrations, let's run them with the following command:

You should see the following output if it succeded:

If we look at our database, we see two tables: schema_migrations and users. The schema_migrations keeps track of our migrations and the order they were executed. This allows Ecto to rollback migrations by using the mix ecto.rollback command.

Let's move on to the credentials migration. I won't go over how to generate and run migrations since that's been covered. This is how our credentials migration should look like:

Notice that our user_id references the users table. We set the :on_delete to :delete_all which deletes the credentials record when the user record is deleted. Check out the Ecto docs for other supported options. Lastly, we set null to false which prevents the user_id from being null.

We also created an index on the email and a unique index on the user_id columns.See the unique_index and index functions for more info.

Our solutions and challenges migrations don't cover anything new so I will skip them but check out the source code reference.

Let's move on to schemas.

Schemas

Schemas are modules that represent data from our database. They define the table and column mapping, help functions, and changesets.It's our database, but in code.

Creating Schemas

Let's create our first schema by creating a directory inside the lib Deadline 8 0 4 1 download free. directory. We currently don't have a generator command so we have to create the directories and files manually 😥. In our demo app, it will be in lib/getting_started_with_ecto/accounts/user.ex and it looks like this:

We use the schema macro to map the user's table and columns to a struct.We define the name column to a string and the age as an integer. Lastly, we call the timestamps function to generate the :inserted_at and :updated_at timestamp fields.

Schema Relationships

Let's create the credentials Habitify: habit tracker 4 0 2 download free. schema. In our demo app, it will be in lib/getting_started_with_ecto/accounts/user.ex:

The new thing here is how we define our one-to-one relationship with the user schema. We use the belongs_to which does most of the work for us. Notice the first parameter is the name of the relationship and the second parameter is the User schema.

We also want to get the credentials when querying the user, let's add that to our User schema. We will do that by adding has_one(:credential, Credential) to the User schema file.

Post

NOTE: Don't forget to add the GettingStartedWithEcto.Accounts.Credential alias at the top of the file.

If your schema has one-to-many association you can use the has_many. Bulk rename utility rus.

Our solution and challenge schemas don't cover anything new so I will skip them but check out the source code reference.

Let's move on to changesets.

Schema Changesets

The changeset is a function in the schema that allows us to filter and cast our schema fields, as well as track and validate data before it gets to the database.

Defining Changesets

Let's define the changeset function for our user schema file:

We first imported the Ecto.Changeset and then we defined our changeset function with two arguments. The user argument can bea changeset, schema struc, or a tuple with {data, types}. The attrs argument is a map like %{'name' => 'Alan'} or %{name: 'Alan'}.

Let's look at the internals of this function.

In line 17, we piped the user to the cast function which applies the attrs changes to the user given a set of allowed keys. In this example, the keys are [:name, :age] since those are our schema fields. This means that any other values in the attrs map will be ignored and filtered out. Lastly, the cast function returns a changeset if everything was successful.

Line 18 is straightforward. We make the name field required. validate_required has an optional third argumentto add a custom message or trim whitespaces.

Finally, we use the validate_inclusion to validate that our age is between 0 and 120. An optional third argument can be provided for a custom message.

Ecto provides other validation function out of the box, check out the Ecto docs for more.

Let's move on to creating custom validation functions.

Custom Validation Function

We can also define custom functions to validate or change any of the fields in the changeset. Let's create afunction that checks if the word ‘Elixir' is part of the password_hash field.

Define our credentials changeset inside it's appropiate schema file.

This changeset is a little more complex since we have more validation.

In line 4, the :user association gets casts with its own changeset. This allows us to create or update the user associated with the credentials.

In line 6, we call our custom validate_password validator function. This function does a simple check on the stringto determine if the word 'Elixir' appears within the validate_password. It returns the changeset for a valid password, or calls the password_invalid function which returns the changeset with an error.

Custom validator functions are flexible and allow you to validate changesets with more granularity.

Our solution and challenge changeset don't cover anything new so I will skip them, but check out the source code reference.

Ecto 3 0 2 – Post Entries To Weblog Systems Inc

Congratulations 🎉 🎉 🎉

Ecto 3 0 2 – Post Entries To Weblog Systems Pdf

We covered a lot of Ecto concepts in this post. We learned how to create migrations, schemas, and changesets.If you need more information or have a question, feel free to leave a comment or check out the Ecto documentation. On the next post, we will start using our Repo to make database queries.

Ecto 3 0 2 – Post Entries To Weblog Systems Using

Software Engineer @Pluralsight. Interested in distributed systems, machine learning, and the web. Follow me on Twitter.





broken image