DEV Community

Cover image for Getting Started on Elixir and Ecto Part 2
Kenzy Limon
Kenzy Limon

Posted on

3

Getting Started on Elixir and Ecto Part 2

Database Setup and Connection

  • NOTE: config/config.exs credentials should be correct to be able to make a successful database connection.
Run this command : mix ecto.create
Expected response : The database for Taskers.Repo has been created.
Enter fullscreen mode Exit fullscreen mode
  • Generate a migration file for our task table, this is a single step in the process of constructing your database.
Run this command : mix ecto.gen.migration create_tasks

This command will create a migration file in priv/repo/migrations.
Enter fullscreen mode Exit fullscreen mode
defmodule Taskers.Repo.Migrations.CreateTableTasks do
use Ecto.Migration
def change do
create table(:tasks) do
add :user_id, :string
add :last_name, :string
add :age, :integer
add :first_name, :string
add :last_name, :string
add :age, :integer
add :first_name, :string
add :last_name, :string
add :age, :integer
add :first_name, :string
add :last_name, :string
add :age, :integer
end
end
end

When defining the schema, Data Types need to be clearly stated. Types are split into two categories, Primitive types, and Custom types. We will cover the primitive type since its the most used one, in the next section.

NOTE: The naming convention for tables in Ecto databases is to use a pluralized name.

To create tasks table in our database
Run this command : mix ecto.migrate

To undo the changes in the migration incase of misstakes
Run this command : mix ecto.rollback
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay