Skip to content

Instantly share code, notes, and snippets.

View abadongutierrez's full-sized avatar
💻
working

Rafael Gutiérrez abadongutierrez

💻
working
View GitHub Profile
defmodule Ecto101MigrationsSchemas do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
supervisor(Ecto101MigrationsSchemas.Repo, [])
]
defmodule Ecto101MigrationsSchemas.Repo do
use Ecto.Repo, otp_app: :ecto_101_migrations_schemas
end
defmodule Ecto101MigrationsSchemas.Mixfile do
use Mix.Project
# ...
def application do
[applications: [:logger, :ecto, :postgrex],
mod: {Ecto101MigrationsSchemas, []}]
end
# ...
defmodule Ecto101MigrationsSchemas.Mixfile do
use Mix.Project
# ...
defp deps do
[{:postgrex, "~> 0.13.3"},
{:ecto, "~> 2.1.4"}]
end
end
defmodule Ecto101MigrationsSchemas.Employee do
use Ecto.Schema
schema "employee" do
field :first_name, :string
field :last_name, :string
field :hire_date, :date
belongs_to :department, Ecto101MigrationsSchemas.Department
defmodule Ecto101MigrationsSchemas.Department do
use Ecto.Schema
schema "department" do
field :name, :string
has_many :employees, Ecto101MigrationsSchemas.Employee
timestamps()
end
defmodule User do
defstruct name: "John", last_name "Smith", age: 32
end
defmodule Ecto101MigrationsSchemas.Repo.Migrations.EmployeeTable do
use Ecto.Migration
def change do
create table(:employee) do
add :first_name, :text, null: false
add :last_name, :text, null: false
add :hire_date, :date, null: false
add :department_id,
references(:department,
@abadongutierrez
abadongutierrez / department_table.exs
Last active June 9, 2017 03:31
Ecto migration file to create a table department
defmodule Ecto101MigrationsSchemas.Repo.Migrations.DepartmentTable do
use Ecto.Migration
def change do
create table(:department) do
add :name, :text, null: false
timestamps()
end
end
Repo.all(from e in "employee",
select: [e.employee_id, e.first_name, e.last_name])