Skip to content

Commit aa3ef65

Browse files
authored
Merge pull request prisma#4398 from prisma/fix-custom-id-field-name
must not rely on hardcoded name for id field
2 parents 6ecb3e9 + ff37d50 commit aa3ef65

File tree

12 files changed

+45
-109
lines changed

12 files changed

+45
-109
lines changed

server/connectors/api-connector-jdbc/src/main/scala/com/prisma/api/connector/jdbc/database/ImportActions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ trait ImportActions extends BuilderBase with SharedJdbcExtensions {
5353
e.getUpdateCounts.zipWithIndex
5454
.filter(element => element._1 == Statement.EXECUTE_FAILED)
5555
.map { failed =>
56-
val failedId = argsWithIndex.find(_._2 == failed._2).get._1.rootGC.idField.value
56+
val failedId = argsWithIndex.find(_._2 == failed._2).get._1.rootGC.idFieldByName(mutaction.model.idField_!.name).value
5757
s"Failure inserting ${model.dbName} with Id: $failedId. Cause: ${removeConnectionInfoFromCause(e.getCause())}"
5858
}
5959
.toVector

server/connectors/api-connector-jdbc/src/main/scala/com/prisma/api/connector/jdbc/database/NodeActions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ trait NodeActions extends BuilderBase with FilterConditionBuilder with ScalarLis
4040
rs.next()
4141
rs.getId(model)
4242
} else {
43-
argsWithIdIfNecessary.idField
43+
argsWithIdIfNecessary.idFieldByName(model.idField_!.name)
4444
}
4545
}
4646
)

server/connectors/api-connector/src/main/scala/com/prisma/api/connector/NodeSelector.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ object NodeSelector {
1111

1212
case class NodeSelector(model: Model, field: ScalarField, fieldGCValue: GCValue) {
1313
require(field.isUnique, s"NodeSelectors may be only instantiated for unique fields! ${field.name} on ${model.name} is not unique.")
14-
lazy val value = fieldGCValue.value
15-
lazy val fieldName = field.name
16-
lazy val isId: Boolean = field.name == "id"
14+
lazy val value = fieldGCValue.value
15+
lazy val fieldName = field.name
1716
}
1817

1918
object NodeAddress {

server/connectors/api-connector/src/main/scala/com/prisma/api/schema/Errors.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ object APIErrors {
3333
case class DataItemDoesNotExist(model: String, uniqueField: String, value: String)
3434
extends ClientApiError(s"'$model' has no item with $uniqueField '$value'", 3002)
3535

36-
object DataItemDoesNotExist {
37-
def apply(model: String, id: String): DataItemDoesNotExist = DataItemDoesNotExist(model, "id", id)
38-
}
39-
4036
case class DataItemAlreadyExists(modelId: String, id: String) extends ClientApiError(s"'$modelId' already has an item with id '$id'", 3004)
4137

4238
case class ExtraArguments(arguments: List[String], model: String)
@@ -101,7 +97,7 @@ object APIErrors {
10197
extends ClientApiError(s"No Node for the model $modelName with value $value for $fieldName found.", 3039)
10298

10399
case class NodeNotFoundForWhereError(where: NodeSelector)
104-
extends ClientApiError(s"No Node for the model ${where.model.name} with value ${where.value} for ${where.field.name} found.", 3039)
100+
extends ClientApiError(s"No Node for the model ${where.model.name} with value ${where.value} for ${where.field.name} found.", 3039)
105101

106102
case class NullProvidedForWhereError(modelName: String)
107103
extends ClientApiError(s"You provided an invalid argument for the where selector on $modelName.", 3040)
@@ -111,8 +107,12 @@ object APIErrors {
111107

112108
case class NodeSelectorInfo(model: String, field: String, value: GCValue)
113109

114-
case class NodesNotConnectedNative(relationName: String, parentName: String, parentWhere: Option[NodeSelectorInfo], childName: String, childWhere: Option[NodeSelectorInfo])
115-
extends ClientApiError(pathErrorMessageNative(relationName, parentName, parentWhere, childName, childWhere), errorCode = 3041)
110+
case class NodesNotConnectedNative(relationName: String,
111+
parentName: String,
112+
parentWhere: Option[NodeSelectorInfo],
113+
childName: String,
114+
childWhere: Option[NodeSelectorInfo])
115+
extends ClientApiError(pathErrorMessageNative(relationName, parentName, parentWhere, childName, childWhere), errorCode = 3041)
116116

117117
case class RequiredRelationWouldBeViolated(relation: Relation)
118118
extends ClientApiError(
@@ -121,10 +121,10 @@ object APIErrors {
121121
)
122122

123123
case class RequiredRelationWouldBeViolatedNative(relationName: String, modelAName: String, modelBName: String)
124-
extends ClientApiError(
125-
s"The change you are trying to make would violate the required relation '$relationName' between $modelAName and $modelBName",
126-
3042
127-
)
124+
extends ClientApiError(
125+
s"The change you are trying to make would violate the required relation '$relationName' between $modelAName and $modelBName",
126+
3042
127+
)
128128

129129
case class MongoConflictingUpdates(model: String, override val message: String)
130130
extends ClientApiError(

server/libs/gc-values/src/main/scala/com/prisma/gc_values/GcValues.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ object RootGCValue {
2727
}
2828

2929
case class RootGCValue(map: Map[String, GCValue]) extends GCValue {
30-
def idField = map.get("id") match {
31-
case Some(id) => id.asInstanceOf[IdGCValue]
32-
case None => sys.error("There was no field with name 'id'.")
33-
}
3430

3531
def idFieldByName(name: String) = map.get(name) match {
3632
case Some(id) => id.asInstanceOf[IdGCValue]

server/servers/api/src/main/scala/com/prisma/api/import_export/BulkImport.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class BulkImport(project: Project)(implicit apiDependencies: ApiDependencies) {
8181
ImportList(ImportIdentifier(typeName, id), field, gcValue)
8282
}
8383

84+
// TODO: do4gr should think about whether Import/Export works with custom names for id fields. I think it does but that should be validated.
8485
def parseIdGCValue(input: JsObject, model: Model): IdGCValue = model.idField_!.typeIdentifier match {
8586
case TypeIdentifier.UUID => UuidGCValue.parse_!(input.value("id").as[String])
8687
case TypeIdentifier.Cuid => StringIdGCValue(input.value("id").as[String])

server/servers/api/src/main/scala/com/prisma/api/mutactions/SubscriptionEvents.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ object SubscriptionEvents {
3232
}
3333

3434
private def fromUpdateResult(project: Project, mutationId: Id, result: UpdateNodeResult): PublishSubscriptionEvent = {
35+
val model = result.mutaction.model
3536
val previousValues: Map[String, Any] = result.previousValues.data
3637
.filterValues(v => v != NullGCValue && !v.isInstanceOf[RootGCValue])
37-
.toMapStringAny + ("id" -> result.previousValues.id.value)
38-
39-
val model = result.mutaction.model
38+
.toMapStringAny + (model.idField_!.name -> result.previousValues.id.value)
4039

4140
PublishSubscriptionEvent(
4241
project = project,
@@ -52,11 +51,10 @@ object SubscriptionEvents {
5251
}
5352

5453
private def fromDeleteResult(project: Project, mutationId: Id, result: DeleteNodeResult): PublishSubscriptionEvent = {
54+
val model = result.mutaction.model
5555
val previousValues = result.previousValues.data
5656
.filterValues(v => v != NullGCValue && !v.isInstanceOf[RootGCValue])
57-
.toMapStringAny + ("id" -> result.previousValues.id.value)
58-
59-
val model = result.mutaction.model
57+
.toMapStringAny + (model.idField_!.name -> result.previousValues.id.value)
6058

6159
PublishSubscriptionEvent(
6260
project = project,

server/servers/api/src/main/scala/com/prisma/api/mutactions/validation/InputValueValidation.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ object InputValueValidation {
3939
}
4040

4141
def scalarFieldsWithValues(model: Model, args: PrismaArgs): List[ScalarField] = {
42-
model.scalarFields.filter(field => args.getFieldValue(field.name).isDefined).filter(_.name != "id")
42+
// TODO: do4gr should check whether we should indeed filter out the id field here. (bring your own id changed that maybe)
43+
model.scalarFields.filter(field => args.getFieldValue(field.name).isDefined).filter(!_.isId)
4344
}
4445
}

server/servers/api/src/main/scala/com/prisma/subscriptions/resolving/FilteredResolver.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object FilteredResolver extends SangriaExtensions {
2626

2727
def removeTopLevelIdFilter(element: Filter) =
2828
element match {
29-
case e: ScalarFilter => e.field.name != "id"
29+
case e: ScalarFilter => !e.field.isId
3030
case _ => true
3131
}
3232

server/servers/api/src/test/scala/com/prisma/api/ApiTestDatabase.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ case class ApiTestDatabase()(implicit dependencies: TestApiDependencies) extends
5050
runMutaction(CreateModelTable(project, model))
5151

5252
model.scalarNonListFields
53-
.filter(f => f.name != ReservedFields.idFieldName)
53+
.filter(f => !f.isId)
5454
.map(field => CreateColumn(project, model, field))
5555
.foreach(runMutaction)
5656

0 commit comments

Comments
 (0)