Skip to content

Commit b62dc66

Browse files
committed
fix(self-relations): use name for join fields instead of type
1 parent 569bfdc commit b62dc66

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

__tests__/dbml.test.ts

+26-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
datamodelDbmlComments,
44
datamodelDbmlDefaults,
55
datamodelDbmlManyToMany,
6+
datamodelDbmlManyToManySelfRelation,
67
datamodelDbmlRelations,
78
} from './fixtures/dbml.datamodel';
89
import { generateDMMF } from './utils/generateDMMF';
@@ -170,13 +171,13 @@ Table Book {
170171
}
171172
172173
Table CategoryToPost {
173-
categoryId Int [ref: > Category.id]
174-
postId Int [ref: > Post.id]
174+
categoriesId Int [ref: > Category.id]
175+
postsId Int [ref: > Post.id]
175176
}
176177
177178
Table AuthorToBook {
178-
bookId Int [ref: > Book.id]
179-
authorId Int [ref: > Author.id]
179+
booksId Int [ref: > Book.id]
180+
authorsId Int [ref: > Author.id]
180181
}`;
181182

182183
const dbml = generateDBMLSchema(dmmf);
@@ -213,4 +214,25 @@ Table Book {
213214

214215
expect(dbml).toEqual(expectedDbml);
215216
});
217+
218+
test('generating dbml schema with many-to-many self relationship', async () => {
219+
const dmmf = await generateDMMF(datamodelDbmlManyToManySelfRelation);
220+
221+
const expectedDbml = `${autoGeneratedComment}
222+
223+
Table User {
224+
id Int [pk, increment]
225+
followers User
226+
following User
227+
}
228+
229+
Table Followers {
230+
followersId Int [ref: > User.id]
231+
followingId Int [ref: > User.id]
232+
}`;
233+
234+
const dbml = generateDBMLSchema(dmmf);
235+
236+
expect(dbml).toEqual(expectedDbml);
237+
});
216238
});

__tests__/fixtures/dbml.datamodel.ts

+8
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,11 @@ export const datamodelDbmlManyToMany = /* Prisma */ `
101101
authors Author[]
102102
}
103103
`;
104+
105+
export const datamodelDbmlManyToManySelfRelation = /* Prisma */ `
106+
model User {
107+
id Int @id @default(autoincrement())
108+
followers User[] @relation("Followers")
109+
following User[] @relation("Followers")
110+
}
111+
`;

src/generator/many-to-many-tables.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function generateJoinFields(field: DMMF.Field[], models: DMMF.Model[]): string {
4343
}
4444

4545
function joinField(field: DMMF.Field, models: DMMF.Model[]): string {
46-
return ` ${field.type.toLowerCase()}Id ${getJoinIdType(
46+
return ` ${field.name.toLowerCase()}Id ${getJoinIdType(
4747
field,
4848
models
4949
)} [ref: > ${field.type}.${field.relationToFields![0]}]`;

0 commit comments

Comments
 (0)