使用 with
意味着表之间存在一对多关系。
因此,如果一个 one
用户拥有 many
帖子,则可以如下使用 with
:
users: {
count: 2,
with: {
posts: 3,
},
},
使用 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" 表没有引用 "users" 表,或者
你没有在 seed 函数的 schema 中包含一对多关系。
你不能在 users.with 对象中指定 "posts" 作为参数。
你有几种方式解决此错误:
posts
表的 authorId
列中添加对 users
表的引用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();
// 运行上述填充脚本将填充数据库如下值
`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();
// 运行上述填充脚本将填充数据库如下值
`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" 表没有引用 "users" 表,或者
你没有在 seed 函数的 schema 中包含一对多关系。
你不能在 users.with 对象中指定 "posts" 作为参数。
你的 schema 中 posts
表引用了 users
表,
.
.
.
export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
content: text('content'),
authorId: integer('author_id').notNull().references(() => users.id),
});
换句话说,你有一个一对多关系,其中一个用户可以拥有多篇帖子。
但是,在填充脚本中,你试图为一篇帖子生成三个(多个)用户。
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();
// 运行上述填充脚本将填充数据库如下值
`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" 表存在自引用。
你不能在 users.with 对象中指定 "users" 作为参数。
你的 schema 中 users
表引用了自己,
.
.
.
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name'),
reportsTo: integer('reports_to').references((): AnyPgColumn => users.id),
});
换句话说,你有一个一对一关系,一个用户只能对应一个用户。
但是,在填充脚本中,你试图为一个用户生成三个(多个)用户,这是不可能的。
users: {
count: 2,
with: {
users: 3,
},
},