File tree Expand file tree Collapse file tree 1 file changed +32
-2
lines changed
Expand file tree Collapse file tree 1 file changed +32
-2
lines changed Original file line number Diff line number Diff line change 196196
197197### The migration
198198
199- It applies to both join tables and schemas.
200-
201199```elixir
202200defmodule 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),
You can’t perform that action at this time.
0 commit comments