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();
위의 시딩 스크립트를 실행하면 다음과 같은 오류가 발생합니다.
오류: "posts" 테이블이 "users" 테이블을 참조하지 않거나,
시드 함수 스키마에 일대다 관계를 포함하지 않았습니다.
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" 테이블을 참조하지 않거나,
시드 함수 스키마에 일대다 관계를 포함하지 않았습니다.
users.with 객체에서 "posts"를 매개변수로 지정할 수 없습니다.
스키마에서 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();
// 위의 시딩 스크립트를 실행하면 데이터베이스에 아래와 같은 값이 채워집니다.
`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"를 매개변수로 지정할 수 없습니다.
스키마에서 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,
},
},