|
| 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> |
0 commit comments