You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/cheatsheets/associations.cheatmd
+88-1
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Associations
2
2
3
-
In this document, "Internal data" represents data or logic hardcoded into your Elixir code. "External data" means data that comes from the user via forms, APIs, and often need to be normalized, pruned, and validated via `Ecto.Changeset`.
3
+
In this document, "Internal data" represents data or logic hardcoded into your Elixir code. "External data" means data that comes from the user via forms, APIs, and often need to be normalized, pruned, and validated via `Ecto.Changeset`. We also include examples of migrations, according to [`EctoSQL`](https://hexdocs.pm/ecto_sql).
4
4
5
5
## Has many / belongs to
6
6
{: .col-2}
@@ -33,6 +33,33 @@ defmodule Character do
33
33
end
34
34
```
35
35
36
+
### The migration
37
+
38
+
```elixir
39
+
defmodule MyApp.Migrations.CreateMoviesAndCharacters do
40
+
use Ecto.Migration
41
+
42
+
def change do
43
+
create table("movies") do
44
+
add :title, :string, null: false
45
+
add :release_date, :date
46
+
timestamps()
47
+
end
48
+
49
+
# The foreign key is in the belongs_to schema
50
+
create table("characters") do
51
+
add :name, :string, null: false
52
+
add :age, :integer
53
+
add :movie_id,
54
+
references(:movies, on_delete: :delete_all),
55
+
null: false
56
+
57
+
timestamps()
58
+
end
59
+
end
60
+
end
61
+
```
62
+
36
63
## Has one / belongs to
37
64
{: .col-2}
38
65
@@ -63,6 +90,32 @@ defmodule Screenplay do
63
90
end
64
91
```
65
92
93
+
### The migration
94
+
95
+
```elixir
96
+
defmodule MyApp.Migrations.CreateMoviesAndPlays do
97
+
use Ecto.Migration
98
+
99
+
def change do
100
+
create table("movies") do
101
+
add :title, :string, null: false
102
+
add :release_date, :date
103
+
timestamps()
104
+
end
105
+
106
+
# The foreign key is in the belongs_to schema
107
+
create table("screenplays") do
108
+
add :lead_writer, :string, null: false
109
+
add :movie_id,
110
+
references(:movies, on_delete: :delete_all),
111
+
null: false
112
+
113
+
timestamps()
114
+
end
115
+
end
116
+
end
117
+
```
118
+
66
119
## Many to many
67
120
{: .col-2}
68
121
@@ -141,6 +194,40 @@ end
141
194
```
142
195
{: .wrap}
143
196
197
+
### The migration
198
+
199
+
It applies to both join tables and schemas.
200
+
201
+
```elixir
202
+
defmodule MyApp.Migrations.CreateUsersAndOrgs do
203
+
use Ecto.Migration
204
+
205
+
def change do
206
+
create table("users") do
207
+
timestamps()
208
+
end
209
+
210
+
create table("organizations") do
211
+
timestamps()
212
+
end
213
+
214
+
create table("users_organizations", primary_key: false) do
Copy file name to clipboardExpand all lines: guides/introduction/Embedded Schemas.md
+1-2
Original file line number
Diff line number
Diff line change
@@ -2,10 +2,9 @@
2
2
3
3
Embedded schemas allow you to define and validate structured data. This data can live in memory, or can be stored in the database. Some use cases for embedded schemas include:
4
4
5
-
- You are maintaining intermediate-state data, like when UI form fields map onto multiple tables in a database.
5
+
- You are maintaining intermediate-state data, like when UI form fields map onto multiple tables in a database, or to model entities which are not backed by a database, such as a contact form
6
6
7
7
- You are working within a persisted parent schema and you want to embed data that is...
8
-
9
8
- simple, like a map of user preferences inside a User schema.
10
9
- changes often, like a list of product images with associated structured data inside a Product schema.
11
10
- requires complex tracking and validation, like an Address schema inside a User schema.
0 commit comments