Skip to content

Regression in 0.19.0: many-to-many relationships without explicit back-ref no longer generate the back-reference field on the related model #355

@borisno2

Description

@borisno2

Summary

In OpenSaaS Stack 0.18.x, a relationship({ ref: 'User', many: true }) field (many-to-many with only one side defined) would automatically generate an implicit back-reference field on the related model. In 0.19.0, this auto-generation has stopped, causing a Prisma schema validation error.

Regression

0.18.x behaviour (working)

Given:

// textMessage.ts
readBy: relationship({
  ref: 'User',
  many: true,
})

The generator produced in schema.prisma:

model TextMessage {
  readBy User[] @relation("TextMessage_readBy")
}

model User {
  readByMessages TextMessage[] @relation("TextMessage_readBy")  // auto-generated back-ref
}

0.19.0 behaviour (broken)

Same config now produces:

model TextMessage {
  readBy User[] @relation("TextMessage_readBy")
}

model User {
  // readByMessages field is MISSING
}

This triggers a Prisma validation error:

Error validating field `readBy` in model `TextMessage`: The relation field `readBy` on model `TextMessage`
is missing an opposite relation field on the model `User`.
Either run `prisma format` or add it manually.

Steps to Reproduce

  1. Create a list with a many-to-many relationship where only one side is defined:
    const TextMessage = list({
      fields: {
        readBy: relationship({ ref: 'User', many: true }),
      },
    })
  2. Run opensaas generate
  3. Observe Prisma validation error

Expected Behaviour

Either:

  1. Restore the auto-generated back-reference: The generator should add an implicit from_<List>_<field> back-reference field on the related model (as 0.18.x did), or
  2. Document the breaking change: If the new behaviour intentionally requires explicit back-refs on both sides, document the migration path with a clear error message like "Relationship readBy is many-to-many but User has no back-reference field pointing to TextMessage.readBy"

Additional Context

  • This affects any migration from Keystone 6 where many-to-many relationships had only one side defined (Keystone 6 similarly auto-generated back-references)
  • Workaround: explicitly define the back-ref field on the related model AND update the ref to point to it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions