Skip to content

Commit

Permalink
fix: include opts when holds foreign key
Browse files Browse the repository at this point in the history
  • Loading branch information
farreldarian committed Jun 24, 2024
1 parent 8af39e2 commit d160465
Showing 1 changed file with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,47 +97,59 @@ function createRelation(input: {
}
}

function holdsForeignKey(args: {
field: PrismaRelationField
model: DMMF.Model
}) {
const { field, model } = args
return model.fields.some((f) =>
field.relationFromFields.some((from) => f.name === from)
)
}

function getOneToOneOrManyRelation(
field: PrismaRelationField,
ctx: GenerateTableRelationsInput
) {
if (hasReference(field)) {
const opts = createRelationOpts({
relationName: field.relationName,
from: {
modelVarName: getModelVarName(ctx.modelModule.model),
fieldNames: field.relationFromFields,
},
to: {
modelVarName: getModelVarName(field.type),
fieldNames: field.relationToFields,
},
})
return createRelation({
referenceModelVarName: getModelVarName(field.type),
opts,
opts: holdsForeignKey({ field, model: ctx.modelModule.model })
? createRelationOpts({
relationName: field.relationName,
from: {
modelVarName: getModelVarName(ctx.modelModule.model),
fieldNames: field.relationFromFields,
},
to: {
modelVarName: getModelVarName(field.type),
fieldNames: field.relationToFields,
},
})
: undefined,
})
}

// For disambiguating relation

const opposingModel = findOpposingRelationModel(field, ctx.datamodel)
const opposingField = findOpposingRelationField(field, opposingModel)
const opts = createRelationOpts({
relationName: field.relationName,
from: {
modelVarName: getModelVarName(ctx.modelModule.model),
fieldNames: opposingField.relationToFields,
},
to: {
modelVarName: getModelVarName(field.type),
fieldNames: opposingField.relationFromFields,
},
})

return createRelation({
referenceModelVarName: getModelVarName(field.type),
opts,
opts: holdsForeignKey({ field, model: ctx.modelModule.model })
? createRelationOpts({
relationName: field.relationName,
from: {
modelVarName: getModelVarName(ctx.modelModule.model),
fieldNames: opposingField.relationToFields,
},
to: {
modelVarName: getModelVarName(field.type),
fieldNames: opposingField.relationFromFields,
},
})
: undefined,
})
}

Expand Down

0 comments on commit d160465

Please sign in to comment.