Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2673 from prismagraphql/bugfix-related-field
Browse files Browse the repository at this point in the history
bugfix: related field must not blow up when the related field has the same name
  • Loading branch information
mavilein committed Jun 21, 2018
2 parents d6197f8 + 504d2aa commit 0e84142
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.prisma.shared.models

import com.prisma.deploy.connector.MissingBackRelations
import com.prisma.shared.schema_dsl.SchemaDsl
import org.scalatest.{FlatSpec, Matchers}

class ModelsSpec extends FlatSpec with Matchers {
"a related field" should "be found when the related fields have the same name" in {
val builder = SchemaDsl()
val model1 = builder.model("Model1")
val model2 = builder.model("Model2")
model1.oneToOneRelation("field", "field", model2)
val project = builder.buildProject()
val withBackRelations = MissingBackRelations.add(project.schema) // should not blow up

withBackRelations.allRelationFields.foreach { rf =>
rf.relatedField // let's see whether this blows up
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ case class RelationField(
}

lazy val relatedField: RelationField = {
relatedModel_!.relationFields.find { field =>
val relation = field.relation
val isTheSameField = field.name == this.name
val isTheSameRelation = relation.relationTableName == this.relation.relationTableName
isTheSameRelation && !isTheSameField
}.get
relatedModel_!.relationFields
.find { field =>
val relation = field.relation
val isTheSameField = field == this
val isTheSameRelation = relation == this.relation
isTheSameRelation && !isTheSameField
}
.getOrElse {
sys.error(s"Could not find related field of $name on model ${model.name}. Relation is ${relation.name}")
}
}

lazy val oppositeRelationSide: RelationSide.Value = {
Expand Down

0 comments on commit 0e84142

Please sign in to comment.