Skip to content

Commit a86729c

Browse files
talionwarclaude
andcommitted
fix(schema): detect many-to-many relations in Prisma parser
The type 'many-to-many' existed in SchemaRelation but was never assigned. Array fields without explicit FK (fkFields) are now correctly classified as many-to-many instead of one-to-many, for both @relation and implicit relation detection. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 23245ac commit a86729c

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

src/schema/prisma-parser.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ export function parsePrismaSchema(schemaPath: string): PrismaParseResult {
9898
const fkFieldsMatch = relBody.match(/fields:\s*\[([^\]]+)\]/);
9999
const referencesMatch = relBody.match(/references:\s*\[([^\]]+)\]/);
100100

101+
// Detect M:N: array field without explicit FK fields (implicit many-to-many)
102+
const isManyToMany = isArray && !fkFieldsMatch;
101103
relations.push({
102104
field: fieldName,
103105
target: fieldType,
104-
type: isArray ? 'one-to-many' : 'one-to-one',
106+
type: isManyToMany ? 'many-to-many' : isArray ? 'one-to-many' : 'one-to-one',
105107
onDelete: onDeleteMatch?.[1],
106108
fkFields: fkFieldsMatch?.[1].split(',').map((s) => s.trim()),
107109
references: referencesMatch?.[1].split(',').map((s) => s.trim()),
@@ -116,10 +118,11 @@ export function parsePrismaSchema(schemaPath: string): PrismaParseResult {
116118
) {
117119
if (!relations.some((r) => r.field === fieldName)) {
118120
const isArray = line.includes('[]');
121+
const isManyToMany = isArray && !line.includes('@relation');
119122
relations.push({
120123
field: fieldName,
121124
target: fieldType,
122-
type: isArray ? 'one-to-many' : 'one-to-one',
125+
type: isManyToMany ? 'many-to-many' : isArray ? 'one-to-many' : 'one-to-one',
123126
});
124127
}
125128
}

0 commit comments

Comments
 (0)