Skip to content

Commit ed8996a

Browse files
Seed guide (#468)
* v1 for seed * partly added generators in seed-overview * formatting * added rest of generators and complex example to seed-overview * Fixes for seed docs * Restructure * added tabs * small fixes * Add a few changes to docs * Finish docs * added arraySize in generators doc * seed-versioning * Update seed versioning docs * seeding with partially exposed schema * seed-guide fixes * Small guide fixes --------- Co-authored-by: AndriiSherman <[email protected]>
1 parent 280dab6 commit ed8996a

File tree

3 files changed

+150
-5
lines changed

3 files changed

+150
-5
lines changed

src/content/docs/guides/_map.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
["point-datatype-psql", "Point datatype in PostgreSQL"],
2020
["postgis-geometry-point", "PostGIS geometry point"],
2121
["postgresql-local-setup", "Local setup of PostgreSQL"],
22-
["mysql-local-setup", "Local setup of MySQL"]
22+
["mysql-local-setup", "Local setup of MySQL"],
23+
["seeding-with-partially-exposed-tables", "Seeding Partially Exposed Tables with Foreign Key"]
2324
]
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
title: Seeding Partially Exposed Tables with Foreign Key
3+
slug: seeding-with-partially-exposed-tables
4+
---
5+
6+
import IsSupportedChipGroup from "@mdx/IsSupportedChipGroup.astro";
7+
import Prerequisites from "@mdx/Prerequisites.astro";
8+
import CodeTabs from '@mdx/CodeTabs.astro';
9+
import CodeTab from '@mdx/CodeTab.astro';
10+
import Callout from '@mdx/Callout.astro';
11+
12+
<IsSupportedChipGroup chips={{PostgreSQL: true, MySQL: true, SQLite: true}}/>
13+
14+
<Prerequisites>
15+
- Get started with [PostgreSQL](/docs/get-started-postgresql), [MySQL](/docs/get-started-mysql) or [SQLite](/docs/get-started-sqlite)
16+
- Get familiar with [Drizzle Seed](/docs/seed-overview)
17+
</Prerequisites>
18+
19+
## Example 1
20+
Let's assume you are trying to seed your database using the seeding script and schema shown below.
21+
<CodeTabs items={["index.ts", "schema.ts"]}>
22+
<CodeTab>
23+
```ts
24+
import { bloodPressure } from './schema.ts';
25+
26+
async function main() {
27+
const db = drizzle(...);
28+
await seed(db, { bloodPressure });
29+
}
30+
main();
31+
32+
```
33+
</CodeTab>
34+
35+
<CodeTab>
36+
```ts copy {10}
37+
import { serial, pgTable, integer, doublePrecision } from "drizzle-orm/pg-core";
38+
39+
export const users = pgTable("users", {
40+
id: serial("id").primaryKey(),
41+
});
42+
43+
export const bloodPressure = pgTable("bloodPressure", {
44+
bloodPressureId: serial().primaryKey(),
45+
pressure: doublePrecision(),
46+
userId: integer().references(() => users.id).notNull(),
47+
})
48+
```
49+
</CodeTab>
50+
</CodeTabs>
51+
If the `bloodPressure` table has a not-null constraint on the `userId` column, running the seeding script will cause an error.
52+
53+
```
54+
Error: Column 'userId' has not null constraint,
55+
and you didn't specify a table for foreign key on column 'userId' in 'bloodPressure' table.
56+
```
57+
58+
<Callout title='What does it mean?'>
59+
This means we can't fill the `userId` column with Null values due to the not-null constraint on that column.
60+
Additionally, you didn't expose the `users` table to the `seed` function schema, so we can't generate `users.id` to populate the `userId` column with these values.
61+
</Callout>
62+
63+
64+
At this point, you have several options to resolve the error:
65+
- You can remove the not-null constraint from the `userId` column;
66+
- You can expose `users` table to `seed` function schema
67+
```ts
68+
await seed(db, { bloodPressure, users });
69+
```
70+
- You can [refine](/docs/guides/seeding-with-partially-exposed-tables#refining-the-userid-column-generator) the `userId` column generator;
71+
72+
## Example 2
73+
74+
<CodeTabs items={["index.ts", "schema.ts"]}>
75+
<CodeTab>
76+
```ts
77+
import { bloodPressure } from './schema.ts';
78+
79+
async function main() {
80+
const db = drizzle(...);
81+
await seed(db, { bloodPressure });
82+
}
83+
main();
84+
85+
```
86+
</CodeTab>
87+
88+
<CodeTab>
89+
```ts copy {10}
90+
import { serial, pgTable, integer, doublePrecision } from "drizzle-orm/pg-core";
91+
92+
export const users = pgTable("users", {
93+
id: serial("id").primaryKey(),
94+
});
95+
96+
export const bloodPressure = pgTable("bloodPressure", {
97+
bloodPressureId: serial().primaryKey(),
98+
pressure: doublePrecision(),
99+
userId: integer().references(() => users.id),
100+
})
101+
```
102+
</CodeTab>
103+
</CodeTabs>
104+
105+
By running the seeding script above you will see a warning
106+
```
107+
Column 'userId' in 'bloodPressure' table will be filled with Null values
108+
because you specified neither a table for foreign key on column 'userId'
109+
nor a function for 'userId' column in refinements.
110+
```
111+
<Callout title='What does it mean?'>
112+
This means you neither provided the `users` table to the `seed` function schema nor refined the `userId` column generator.
113+
As a result, the `userId` column will be filled with Null values.
114+
</Callout>
115+
Then you will have two choices:
116+
- If you're okay with filling the `userId` column with Null values, you can ignore the warning;
117+
118+
- Otherwise, you can [refine](/docs/guides/seeding-with-partially-exposed-tables#refining-the-userid-column-generator) the `userId` column generator.
119+
120+
## Refining the `userId` column generator
121+
Doing so requires the `users` table to already have IDs such as 1 and 2 in the database.
122+
<CodeTabs items={["index.ts"]}>
123+
<CodeTab>
124+
```ts copy {8}
125+
import { bloodPressure } from './schema.ts';
126+
127+
async function main() {
128+
const db = drizzle(...);
129+
await seed(db, { bloodPressure }).refine((funcs) => ({
130+
bloodPressure: {
131+
columns: {
132+
userId: funcs.valuesFromArray({ values: [1, 2] })
133+
}
134+
}
135+
}));
136+
}
137+
main();
138+
139+
```
140+
</CodeTab>
141+
</CodeTabs>

src/content/docs/seed-versioning.mdx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Tab from "@mdx/Tab.astro";
22
import Tabs from "@mdx/Tabs.astro";
33
import Callout from "@mdx/Callout.astro";
4+
import TableWrapper from "@mdx/TableWrapper.astro";
45

56
# Versioning
67

@@ -18,10 +19,12 @@ await seed(db, schema, { version: '2' });
1819
```
1920

2021
## History
21-
| | api version | npm version | Changed generators |
22-
| :-: | :--------------: | :--------------: | :-------------: |
23-
| | `v1` | `0.1.1` | |
24-
| | `v2 (LTS) ` | `0.2.1` |`string()`, `interval({ isUnique: true })` |
22+
<TableWrapper>
23+
| api version | npm version | Changed generators |
24+
| :-------------- | :-------------- | :------------- |
25+
| `v1` | `0.1.1` | |
26+
| `v2 (LTS) ` | `0.2.1` |`string()`, `interval({ isUnique: true })` |
27+
</TableWrapper>
2528

2629
<Callout collapsed="How it works under the hood?">
2730
> This is not an actual API change; it is just an example of how we will proceed with `drizzle-seed` versioning.

0 commit comments

Comments
 (0)