使用 with
意味着表之间存在一对多关系。
因此,如果 one
用户有 many
条帖子,你可以按如下方式使用 with
:
users: {
count: 2,
with: {
posts: 3,
},
},
开始使用 PostgreSQL、MySQL 或 SQLite
熟悉 一对多关系
熟悉 Drizzle Seed
使用 with
意味着表之间存在一对多关系。
因此,如果 one
用户有 many
条帖子,你可以按如下方式使用 with
:
users: {
count: 2,
with: {
posts: 3,
},
},
import { users, posts } from './schema.ts';
async function main() {
const db = drizzle(...);
await seed(db, { users, posts }).refine(() => ({
users: {
count: 2,
with: {
posts: 3,
},
},
}));
}
main();
运行上述种子脚本将导致错误。
Error: "posts" table doesn't have a reference to "users" table or
you didn't include your one-to-many relation in the seed function schema.
You can't specify "posts" as parameter in users.with object.
你可以通过多种方式解决错误:
posts
表中 authorId
列的引用。import { users, posts } from './schema.ts';
async function main() {
const db = drizzle(...);
await seed(db, { users, posts }).refine(() => ({
users: {
count: 2,
with: {
posts: 3,
},
},
}));
}
main();
// Running the seeding script above will fill you database with values shown below
`users`
| id | name |
| -- | -------- |
| 1 | 'Melanny' |
| 2 | 'Elvera' |
`posts`
| id | content | author_id |
| -- | --------------------- | --------- |
| 1 | 'tf02gUXb0LZIdEg6SL' | 2 |
| 2 | 'j15YdT7Sma' | 2 |
| 3 | 'LwwvWtLLAZzIpk' | 1 |
| 4 | 'mgyUnBKSrQw' | 1 |
| 5 | 'CjAJByKIqilHcPjkvEw' | 2 |
| 6 | 'S5g0NzXs' | 1 |
import { users, posts, postsRelations } from './schema.ts';
async function main() {
const db = drizzle(...);
await seed(db, { users, posts, postsRelations }).refine(() => ({
users: {
count: 2,
with: {
posts: 3,
},
},
}));
}
main();
// Running the seeding script above will fill you database with values shown below
`users`
| id | name |
| -- | -------- |
| 1 | 'Melanny' |
| 2 | 'Elvera' |
`posts`
| id | content | author_id |
| -- | --------------------- | --------- |
| 1 | 'tf02gUXb0LZIdEg6SL' | 2 |
| 2 | 'j15YdT7Sma' | 2 |
| 3 | 'LwwvWtLLAZzIpk' | 1 |
| 4 | 'mgyUnBKSrQw' | 1 |
| 5 | 'CjAJByKIqilHcPjkvEw' | 2 |
| 6 | 'S5g0NzXs' | 1 |
import { users, posts } from './schema.ts';
async function main() {
const db = drizzle(...);
await seed(db, { users, posts }).refine(() => ({
posts: {
count: 2,
with: {
users: 3,
},
},
}));
}
main();
运行上述种子脚本将导致错误。
Error: "posts" table doesn't have a reference to "users" table or
you didn't include your one-to-many relation in the seed function schema.
You can't specify "posts" as parameter in users.with object.
你的架构中有一个 posts
表引用了一个 users
表。
.
.
.
export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
content: text('content'),
authorId: integer('author_id').notNull().references(() => users.id),
});
或者换句话说,你有一个一对多关系,其中 one
用户可以发布 many
个帖子。
但是,在你的种子脚本中,你尝试为 one
帖子生成 3 个(many
)用户。
posts: {
count: 2,
with: {
users: 3,
},
},
要解决此错误,你可以修改你的种子脚本,如下所示如下所示:
import { users, posts, postsRelations } from './schema.ts';
async function main() {
const db = drizzle(...);
await seed(db, { users, posts, postsRelations }).refine(() => ({
users: {
count: 2,
with: {
posts: 3,
},
},
}));
}
main();
// Running the seeding script above will fill you database with values shown below
`users`
| id | name |
| -- | -------- |
| 1 | 'Melanny' |
| 2 | 'Elvera' |
`posts`
| id | content | author_id |
| -- | --------------------- | --------- |
| 1 | 'tf02gUXb0LZIdEg6SL' | 2 |
| 2 | 'j15YdT7Sma' | 2 |
| 3 | 'LwwvWtLLAZzIpk' | 1 |
| 4 | 'mgyUnBKSrQw' | 1 |
| 5 | 'CjAJByKIqilHcPjkvEw' | 2 |
| 6 | 'S5g0NzXs' | 1 |
import { users } from './schema.ts';
async function main() {
const db = drizzle(...);
await seed(db, { users }).refine(() => ({
users: {
count: 2,
with: {
users: 3,
},
},
}));
}
main();
运行上述种子脚本将导致错误。
Error: "users" table has self reference.
You can't specify "users" as parameter in users.with object.
你的架构中有一个 users
表引用了一个 users
表。
.
.
.
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name'),
reportsTo: integer('reports_to').references((): AnyPgColumn => users.id),
});
或者换句话说,你拥有一对一关系,其中 one
用户只能拥有 one
用户。
但是,在你的种子脚本中,你尝试为 one
用户生成 3 个(many
)用户,这是不可能的。
users: {
count: 2,
with: {
users: 3,
},
},