Prisma1: Updating existing relations can lead to an error

Created on 28 Feb 2018  路  11Comments  路  Source: prisma/prisma1

Bug Report

Current behavior

prisma-database_1  | java.lang.IllegalArgumentException: requirement failed: This method must only be called for unambiguous relations!
prisma-database_1  |    at scala.Predef$.require(Predef.scala:277)
prisma-database_1  |    at com.prisma.shared.models.Schema.getUnambiguousRelationThatConnectsModels_$bang(Models.scala:114)
prisma-database_1  |    at com.prisma.deploy.migration.inference.MigrationStepsInferrerImpl.$anonfun$relationsToUpdate$3(MigrationStepsInferrer.scala:178)
prisma-database_1  |    at scala.Option.orElse(Option.scala:289)
prisma-database_1  |    at com.prisma.deploy.migration.inference.MigrationStepsInferrerImpl.$anonfun$relationsToUpdate$2(MigrationStepsInferrer.scala:178)
prisma-database_1  |    at scala.collection.TraversableLike.$anonfun$flatMap$1(TraversableLike.scala:241)
prisma-database_1  |    at scala.collection.Iterator.foreach(Iterator.scala:929)
prisma-database_1  |    at scala.collection.Iterator.foreach$(Iterator.scala:929)
prisma-database_1  |    at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)

Reproduction

add 2 relations between 2 types which were not there before

i.e.

type Mote {
 # creator: User!
 members: [User!]!
}

type User {
}

to

type Mote {
 creator: User! @relation(name: "Creator")
 members: [User!]! @relation(name: "MemberOf")
}

type User {
 motes: [Mote!]! @relation(name: "Creator")
 memberOf: [Mote!]! @relation(name: "MemberOf")
}

it works if you have it in the initial deploy

bu2-confirmed

Most helpful comment

For anyone stumbling upon this while plotting out a fresh schema, just remove the lines with relations, do a prisma deploy to clean up your db, then add the lines back in and do another prisma deploy.

Prisma has trouble updating many many-to-many relationships.

Example, many users can belong to a Group or Organization as a member and/or owner:

type User {
  groups: [Group!]!
  ownedGroups: [Group!]!
  organization: [Organization!]!
  ownedOrganization: [Organization!]!
}

type Group {
  members: [User!]!
  owners: [User!]!
}

type Organization {
  members: [User!]!
  owners: [User!]!
}

^ will throw an error because it needs more data to determine what table the duplicate field values in User should point to.

When you add the appropriate @relations, $ prisma deploy throws because it can't update the tables.

So just remove all those lines (if you're on a fresh db), then prisma deploy, then add the lines back in (cmd+z), then prisma deploy again, to create fresh tables with fresh relations.

If you're working on an existing db with precious data, you'll have to manually tweak everything.

All 11 comments

Hey @ForsakenHarmony, thanks for reporting this. The error occurs because we cannot infer with complete certainty which of the two named relations in the second schema is an update of the relation in the first one. We could try to infer it from the fieldName, but that might have changed as well.

I will implement a proper error for this case. The solution for you, that I'll probably also provide in the error message for this case, is to use a relation directive on the first schema and name the relation. Than we are sure that you want us to update that relation and not delete and recreate a new one.

So in your case this should work:

type Mote {
 # creator: User!
 members: [User!]! @relation(name: "MemberOf")
}

type User {
}

to

```graphql
type Mote {
creator: User! @relation(name: "Creator")
members: [User!]! @relation(name: "MemberOf")
}

type User {
motes: [Mote!]! @relation(name: "Creator")
memberOf: [Mote!]! @relation(name: "MemberOf")
}

oh ok, I'll try that, thanks

Well now it didn't create the corresponding table

Caused by: java.sql.SQLException: Table 'productive@dev._MoteCreator' doesn't exist

now I can't even undo it

managed to undo it by manually editing the database

but prisma import required me to set a lot of stuff as optional to work, it doesn't like relations

That sounds like a bug :-)

Would be super helpful if you can provide a minimal example with required relations that break export/import and open a new issue.

Hey @ForsakenHarmony, is this what you're encountering: https://github.com/graphcool/prisma/issues/1767?

yes

Thanks, we'll look into a fix soon. Let's continue our discussion there 馃檪

For anyone stumbling upon this while plotting out a fresh schema, just remove the lines with relations, do a prisma deploy to clean up your db, then add the lines back in and do another prisma deploy.

Prisma has trouble updating many many-to-many relationships.

Example, many users can belong to a Group or Organization as a member and/or owner:

type User {
  groups: [Group!]!
  ownedGroups: [Group!]!
  organization: [Organization!]!
  ownedOrganization: [Organization!]!
}

type Group {
  members: [User!]!
  owners: [User!]!
}

type Organization {
  members: [User!]!
  owners: [User!]!
}

^ will throw an error because it needs more data to determine what table the duplicate field values in User should point to.

When you add the appropriate @relations, $ prisma deploy throws because it can't update the tables.

So just remove all those lines (if you're on a fresh db), then prisma deploy, then add the lines back in (cmd+z), then prisma deploy again, to create fresh tables with fresh relations.

If you're working on an existing db with precious data, you'll have to manually tweak everything.

Was this page helpful?
0 / 5 - 0 ratings