Skip to content

Commit 7504d0f

Browse files
authored
Updates many-to-many guide (#4704)
1 parent a75cefa commit 7504d0f

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

guides/cheatsheets/associations.cheatmd

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,6 @@ end
196196

197197
### The migration
198198

199-
It applies to both join tables and schemas.
200-
201199
```elixir
202200
defmodule MyApp.Migrations.CreateMoviesAndActors do
203201
use Ecto.Migration
@@ -210,7 +208,39 @@ defmodule MyApp.Migrations.CreateMoviesAndActors do
210208
create table("actors") do
211209
timestamps()
212210
end
211+
end
212+
end
213+
```
214+
215+
#### Without a join schema, the join table can only have the foreign keys
216+
217+
```elixir
218+
defmodule MyApp.Migrations.CreateJoinTable do
219+
use Ecto.Migration
213220

221+
def change do
222+
create table("movies_actors", primary_key: false) do
223+
add :movie_id,
224+
references(:movies, on_delete: :delete_all),
225+
null: false
226+
227+
add :actor_id,
228+
references(:actors, on_delete: :delete_all),
229+
null: false
230+
end
231+
232+
create unique_index(:movies_actors, [:movie_id, :actor_id])
233+
end
234+
end
235+
```
236+
237+
#### With a join schema, the join table can have other columns like timestamps
238+
239+
```elixir
240+
defmodule MyApp.Migrations.CreateJoinTable do
241+
use Ecto.Migration
242+
243+
def change do
214244
create table("movies_actors", primary_key: false) do
215245
add :movie_id,
216246
references(:movies, on_delete: :delete_all),

0 commit comments

Comments
 (0)