Drizzle | with 옵션으로 시딩
PostgreSQL
MySQL
SQLite
This guide assumes familiarity with:
주의

with를 사용하는 것은 테이블 간 일대다 관계를 의미합니다.

따라서 하나의 사용자가 여러 게시글을 가지는 경우, 다음과 같이 with를 사용할 수 있습니다:

users: {
    count: 2,
    with: {
        posts: 3,
    },
},

예제 1

index.ts
schema.ts
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.

오류를 해결하기 위한 몇 가지 방법이 있습니다:

index.ts
schema.ts
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     |
index.ts
schema.ts
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     |

예제 2

index.ts
schema.ts
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();

Running the seeding script above will cause an error.

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.
왜 그럴까요?

스키마에 users 테이블을 참조하는 posts 테이블이 있습니다.

.
.
.
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     |

예제 3

index.ts
schema.ts
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),
});

다시 말해, 하나의 사용자가 하나의 사용자만 가질 수 있는 일대일 관계입니다.

그러나 시딩 스크립트에서는 하나의 사용자에 대해 3개(여러)의 사용자를 생성하려고 시도하고 있는데, 이는 불가능합니다.

users: {
    count: 2,
    with: {
        users: 3,
    },
},