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 #4747 from prisma/CustomIdsWithRelationsOnPostgres…
Browse files Browse the repository at this point in the history
…AndMySql

Fix Relationtable bug in Postgres and SQLite
  • Loading branch information
do4gr authored Jul 19, 2019
2 parents 39727ee + 40112cf commit ff91f1c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ case class PostgresJdbcDeployDatabaseMutationBuilder(
#$aColSql,
#$bColSql,
FOREIGN KEY ("#$modelAColumn") REFERENCES #${qualify(project.dbName, modelA.dbName)} (#${qualify(modelA.dbNameOfIdField_!)}) ON DELETE CASCADE,
FOREIGN KEY ("#$modelBColumn") REFERENCES #${qualify(project.dbName, modelB.dbName)} (#${qualify(modelA.dbNameOfIdField_!)}) ON DELETE CASCADE
FOREIGN KEY ("#$modelBColumn") REFERENCES #${qualify(project.dbName, modelB.dbName)} (#${qualify(modelB.dbNameOfIdField_!)}) ON DELETE CASCADE
);"""

val modernTableCreate = sqlu"""
CREATE TABLE #${qualify(project.dbName, relationTableName)} (
#$aColSql,
#$bColSql,
FOREIGN KEY ("#$modelAColumn") REFERENCES #${qualify(project.dbName, modelA.dbName)} (#${qualify(modelA.dbNameOfIdField_!)}) ON DELETE CASCADE,
FOREIGN KEY ("#$modelBColumn") REFERENCES #${qualify(project.dbName, modelB.dbName)} (#${qualify(modelA.dbNameOfIdField_!)}) ON DELETE CASCADE
FOREIGN KEY ("#$modelBColumn") REFERENCES #${qualify(project.dbName, modelB.dbName)} (#${qualify(modelB.dbNameOfIdField_!)}) ON DELETE CASCADE
);"""

val tableCreate = relation.manifestation match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ case class SQLiteJdbcDeployDatabaseMutationBuilder(
#$bColSql,
PRIMARY KEY ("id"),
FOREIGN KEY (#$modelAColumn) REFERENCES #${qualify(modelA.dbName)} (#${qualify(modelA.dbNameOfIdField_!)}) ON DELETE CASCADE,
FOREIGN KEY (#$modelBColumn) REFERENCES #${qualify(modelB.dbName)} (#${qualify(modelA.dbNameOfIdField_!)}) ON DELETE CASCADE
FOREIGN KEY (#$modelBColumn) REFERENCES #${qualify(modelB.dbName)} (#${qualify(modelB.dbNameOfIdField_!)}) ON DELETE CASCADE
);"""

val modernTableCreate = sqlu"""
CREATE TABLE #${qualify(project.dbName, relationTableName)} (
#$aColSql,
#$bColSql,
FOREIGN KEY (#$modelAColumn) REFERENCES #${qualify(modelA.dbName)} (#${qualify(modelA.dbNameOfIdField_!)}) ON DELETE CASCADE,
FOREIGN KEY (#$modelBColumn) REFERENCES #${qualify(modelB.dbName)} (#${qualify(modelA.dbNameOfIdField_!)}) ON DELETE CASCADE
FOREIGN KEY (#$modelBColumn) REFERENCES #${qualify(modelB.dbName)} (#${qualify(modelB.dbNameOfIdField_!)}) ON DELETE CASCADE
);"""

val tableCreate = relation.manifestation match {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.prisma.integration

import org.scalatest.{FlatSpec, Matchers}

class DeployingCustomIdNamesSpec extends FlatSpec with Matchers with IntegrationBaseSpec {

"Using Custom Id field names with relations" should "work" in {

val schema =
"""type Person {
| id: ID! @id
| age: Int! @unique
|}"""

val (project, _) = setupProject(schema)

val schema1 =
"""type Game {
| gameId: ID! @id
| createdAt: DateTime! @createdAt
| updatedAt: DateTime! @updatedAt
| lastUpdatedOn: String!
| players: [Player!]! @relation(link: TABLE, name: "GamePlayer")
|}
|
|type Player {
| playerId: ID! @id
| createdAt: DateTime! @createdAt
| updatedAt: DateTime! @updatedAt
| lastUpdatedOn: String!
| games: [Game!]! @relation(name: "GamePlayer")
|}"""

deployServer.deploySchemaThatMustSucceed(project, schema1, 3, true)
}

"Using [typename]id as a field for relations" should "work" in {

val schema =
"""type Person {
| id: ID! @id
| age: Int! @unique
|}"""

val (project, _) = setupProject(schema)

val schema1 =
"""type Contact @db(name: "contact") {
|id: Int! @id
|info: String!
|type: String!
|customerid: Customer
|}
|
|type Customer @db(name: "customer") {
|id: Int! @id
|contact: [Contact]
|firstname: String!
|lastname: String!
|}"""

deployServer.deploySchemaThatMustSucceed(project, schema1, 3, true)
}

}

0 comments on commit ff91f1c

Please sign in to comment.