Drizzle | 외래 키가 있는 부분 노출 테이블 시딩
PostgreSQL
MySQL
SQLite
This guide assumes familiarity with:

예제 1

아래와 같은 시딩 스크립트와 스키마를 사용하여 데이터베이스를 시딩하려고 한다고 가정해봅시다.

index.ts
schema.ts
import { bloodPressure } from './schema.ts';

async function main() {
  const db = drizzle(...);
  await seed(db, { bloodPressure });
}
main();

만약 bloodPressure 테이블의 userId 컬럼에 not-null 제약조건이 있다면, 시딩 스크립트를 실행할 때 오류가 발생합니다.

Error: Column 'userId' has not null constraint,
and you didn't specify a table for foreign key on column 'userId' in 'bloodPressure' table.
무슨 의미인가요?

이는 해당 컬럼의 not-null 제약조건으로 인해 userId 컬럼을 Null 값으로 채울 수 없다는 의미입니다. 또한, seed 함수 스키마에 users 테이블을 노출하지 않았기 때문에 users.id를 생성하여 userId 컬럼을 이 값들로 채울 수 없습니다.

이 시점에서 오류를 해결하기 위한 몇 가지 옵션이 있습니다:

await seed(db, { bloodPressure, users });

예제 2

index.ts
schema.ts
import { bloodPressure } from './schema.ts';

async function main() {
  const db = drizzle(...);
  await seed(db, { bloodPressure });
}
main();

위의 시딩 스크립트를 실행하면 다음과 같은 경고가 표시됩니다

Column 'userId' in 'bloodPressure' table will be filled with Null values
because you specified neither a table for foreign key on column 'userId'
nor a function for 'userId' column in refinements.
무슨 의미인가요?

이는 seed 함수 스키마에 users 테이블을 제공하지 않았고 userId 컬럼 생성기도 세분화하지 않았다는 의미입니다. 그 결과, userId 컬럼이 Null 값으로 채워집니다.

이제 두 가지 선택지가 있습니다:

userId 컬럼 생성기 세분화하기

이를 수행하려면 데이터베이스의 users 테이블에 이미 1과 2와 같은 ID가 있어야 합니다.

index.ts
import { bloodPressure } from './schema.ts';

async function main() {
  const db = drizzle(...);
  await seed(db, { bloodPressure }).refine((funcs) => ({
    bloodPressure: {
      columns: {
        userId: funcs.valuesFromArray({ values: [1, 2] })
      }
    }
  }));
}
main();