Drizzle | Seeding using 'with' option
This guide assumes familiarity with:
Warning
Using with
implies tables to have a one-to-many relationship.
Therefore, if one
user has many
posts, you can use with
as follows:
Example 1
Running the seeding script above will cause an error.
You will have several options to resolve an error:
- You can add reference to the
authorId
column in posts
table in your schema
- You can add one-to-many relation to your schema and include it in the seed function schema
Example 2
Running the seeding script above will cause an error.
Why?
You have a posts
table referencing a users
table in your schema,
or in other words, you have one-to-many relation where one
user can have many
posts.
However, in your seeding script, you’re attempting to generate 3 (many
) users for one
post.
To resolve the error, you can modify your seeding script as follows:
Example 3
Running the seeding script above will cause an error.
Why?
You have a users
table referencing a users
table in your schema,
or in other words, you have one-to-one relation where one
user can have only one
user.
However, in your seeding script, you’re attempting to generate 3 (many
) users for one
user, which is impossible.