使用 with 意味着表具有一对多关系。
因此,如果 一个 用户拥有 多个 帖子,您可以使用 with,如下所示:
users: {
count: 2,
with: {
posts: 3,
},
},使用 with 意味着表具有一对多关系。
因此,如果 一个 用户拥有 多个 帖子,您可以使用 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.您的 schema 中有一个 posts 表引用了 users 表,
.
.
.
export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
content: text('content'),
authorId: integer('author_id').notNull().references(() => users.id),
});换句话说,您有一个一对多关系,其中 一个 用户可以拥有 多个 帖子。
然而,在您的数据填充脚本中,您试图为 一个 帖子生成 3 个 (多个) 用户。
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.您的 schema 中有一个 users 表引用了 users 表,
.
.
.
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name'),
reportsTo: integer('reports_to').references((): AnyPgColumn => users.id),
});换句话说,您有一个一对一关系,其中 一个 用户只能拥有 一个 用户。
然而,在您的数据填充脚本中,您试图为 一个 用户生成 3 个 (多个) 用户,这是不可能的。
users: {
count: 2,
with: {
users: 3,
},
},