Skip to content

Commit d8377e5

Browse files
committed
fixes
1 parent b5c39c7 commit d8377e5

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

drizzle-seed/src/services/SeedService.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export class SeedService {
6666
return table1Order - table2Order;
6767
});
6868

69+
const tableNamesSet = new Set(tables.map((table) => table.name));
6970
const tablesPossibleGenerators: Prettify<
7071
(typeof tablePossibleGenerators)[]
7172
> = tables.map((table) => ({
@@ -211,24 +212,36 @@ export class SeedService {
211212
columnPossibleGenerator.isCyclic = true;
212213
}
213214

214-
if (foreignKeyColumns[col.name]?.table === undefined && col.notNull === true) {
215+
if (
216+
(foreignKeyColumns[col.name]?.table === undefined || !tableNamesSet.has(foreignKeyColumns[col.name]!.table))
217+
&& col.notNull === true
218+
) {
215219
throw new Error(
216220
`Column '${col.name}' has not null contraint,`
217221
+ `\nand you didn't specify a table for foreign key on column '${col.name}' in '${table.name}' table.`
218-
+ `\n For more details, check this: https://orm.drizzle.team/docs/guides/seeding-with-partially-exposed-tables#example-1`,
222+
+ `\n\nFor more details, check this: https://orm.drizzle.team/docs/guides/seeding-with-partially-exposed-tables#example-1`,
219223
);
220224
}
221225

222-
const predicate = (cyclicRelation !== undefined || foreignKeyColumns[col.name]?.table === undefined)
226+
const predicate = (
227+
cyclicRelation !== undefined
228+
|| (
229+
foreignKeyColumns[col.name]?.table === undefined
230+
|| !tableNamesSet.has(foreignKeyColumns[col.name]!.table)
231+
)
232+
)
223233
&& col.notNull === false;
224234

225235
if (predicate === true) {
226-
if (foreignKeyColumns[col.name]?.table === undefined && col.notNull === false) {
236+
if (
237+
(foreignKeyColumns[col.name]?.table === undefined
238+
|| !tableNamesSet.has(foreignKeyColumns[col.name]!.table)) && col.notNull === false
239+
) {
227240
console.warn(
228241
`Column '${col.name}' in '${table.name}' table will be filled with Null values`
229242
+ `\nbecause you specified neither a table for foreign key on column '${col.name}'`
230243
+ `\nnor a function for '${col.name}' column in refinements.`
231-
+ `\nFor more details, check this: https://orm.drizzle.team/docs/guides/seeding-with-partially-exposed-tables#example-2`,
244+
+ `\n\nFor more details, check this: https://orm.drizzle.team/docs/guides/seeding-with-partially-exposed-tables#example-2`,
232245
);
233246
}
234247
columnPossibleGenerator.generator = new generatorsMap.GenerateDefault[0]({ defaultValue: null });

0 commit comments

Comments
 (0)