Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions guides/cheatsheets/associations.cheatmd
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ end

### The migration

It applies to both join tables and schemas.

```elixir
defmodule MyApp.Migrations.CreateMoviesAndActors do
use Ecto.Migration
Expand All @@ -210,7 +208,39 @@ defmodule MyApp.Migrations.CreateMoviesAndActors do
create table("actors") do
timestamps()
end
end
end
```

#### Without a join schema, the join table can only have the foreign keys

```elixir
defmodule MyApp.Migrations.CreateJoinTable do
use Ecto.Migration

def change do
create table("movies_actors", primary_key: false) do
add :movie_id,
references(:movies, on_delete: :delete_all),
null: false

add :actor_id,
references(:actors, on_delete: :delete_all),
null: false
end

create unique_index(:movies_actors, [:movie_id, :actor_id])
end
end
```

#### With a join schema, the join table can have other columns like timestamps

```elixir
defmodule MyApp.Migrations.CreateJoinTable do
use Ecto.Migration

def change do
create table("movies_actors", primary_key: false) do
add :movie_id,
references(:movies, on_delete: :delete_all),
Expand Down
Loading