Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Multi Schema] Same model map different schema #15009

Closed
danstarns opened this issue Aug 25, 2022 · 4 comments · Fixed by prisma/prisma-engines#3255, #15647 or #15798
Closed

[Multi Schema] Same model map different schema #15009

danstarns opened this issue Aug 25, 2022 · 4 comments · Fixed by prisma/prisma-engines#3255, #15647 or #15798
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: internal topic: multiSchema multiple schemas
Milestone

Comments

@danstarns
Copy link
Contributor

Bug description

I am using multiSchema, and I have the following schema:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["multiSchema"]
}

datasource db {
  provider = "postgresql"
  url      = ...
  schemas  = ["base", "transactional"]
}

model User {
  email String
  posts Post[]

  @@map("some_table")
  @@schema("base")
}

model Post {
  title    String
  authorId String
  author   User?  @relation(fields: [authorId], references: [id])

  @@map("some_table")
  @@schema("transactional")
}

The User model belongs to the base schema and it is mapped to the some_table tabel name.

The Post model belongs to the transactional schema and it is mapped to the some_table tabel name.

I get the error:

Error: Get DMMF: Schema parsing - Error while interacting with query-engine-node-api library
Error code: P1012
error: Error validating: The argument `references` must refer only to existing fields in the related model `User`. The following fields do not exist in the related model: id
  -->  schema.prisma:24
   | 
23 |   authorId String
24 |   author   User?  @relation(fields: [authorId], references: [id])
   | 
error: The model with database name "some_table" could not be defined because another model with this name exists: "User"
  -->  schema.prisma:21
   | 
20 | 
21 | model Post {
22 |   title    String
23 |   authorId String
24 |   author   User?  @relation(fields: [authorId], references: [id])
25 | 
26 |   @@map("some_table")
27 |   @@schema("transactional")
28 | }
   | 

How to reproduce

  1. Take the given schema
  2. Run npx prisma migrate dev

Expected behavior

Both models map to the same tabel name some_table however, they are in different schemas and so this should work.

Prisma information

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Node.js version: 18.2.0

Prisma Version

dev
@danstarns danstarns added kind/bug A reported bug. topic: internal team/schema Issue for team Schema. topic: multiSchema multiple schemas bug/2-confirmed Bug has been reproduced and confirmed. labels Aug 25, 2022
@janpio janpio changed the title Multi Schema same model map different schema [Multi Schema] Same model map different schema Aug 27, 2022
@tomhoule
Copy link
Contributor

Can confirm that the name collision is a bug, the mapped names should be qualified with their schema while checking for mapped name collisions. In terms of implementation, HashSet<(Option<String>, String)> instead of HashSet<String>. The extra Option<String> would be the schema name, so the logic does not need to change for prisma schemas without @@schema annotations (they'll just always have None there).

@tomhoule tomhoule self-assigned this Sep 30, 2022
tomhoule added a commit to prisma/prisma-engines that referenced this issue Sep 30, 2022
The validation we have validates that no two models in the whole schema
have the same database name. Database name is defined as the contents of
the model's `@@map` attribute, or the model name if the model name does
not have an `@@map` attribute. It is the name we give to the
table/collection corresponding to the model in the database.

When the `multiSchema` preview feature is turned on, the tables in the
database can be in different schemas, so we have to adapt the
validation to allow multiple models to have the same database name, as
long as they are in a different schema. This is what this commit does.

closes prisma/prisma#15009
tomhoule added a commit to prisma/prisma-engines that referenced this issue Sep 30, 2022
The validation we have validates that no two models in the whole schema
have the same database name. Database name is defined as the contents of
the model's `@@map` attribute, or the model name if the model name does
not have an `@@map` attribute. It is the name we give to the
table/collection corresponding to the model in the database.

When the `multiSchema` preview feature is turned on, the tables in the
database can be in different schemas, so we have to adapt the
validation to allow multiple models to have the same database name, as
long as they are in a different schema. This is what this commit does.

closes prisma/prisma#15009
@janpio janpio added this to the 4.5.0 milestone Sep 30, 2022
Jolg42 added a commit that referenced this issue Oct 4, 2022
@Jolg42
Copy link
Member

Jolg42 commented Oct 4, 2022

Opened a PR to adapt the current test #15647

@Jolg42
Copy link
Member

Jolg42 commented Oct 4, 2022

Reopening until test from #15647 is merged (currently not passing because engine is not up-to-date)

@Jolg42 Jolg42 reopened this Oct 4, 2022
Jolg42 added a commit that referenced this issue Oct 5, 2022
@Jolg42
Copy link
Member

Jolg42 commented Oct 5, 2022

In our dev version it now errors with a different error message with #15647 (comment)

We want to fix this.

Jolg42 added a commit that referenced this issue Oct 11, 2022
Jolg42 added a commit that referenced this issue Oct 14, 2022
Jolg42 added a commit that referenced this issue Oct 14, 2022
…gration

Closes #15009

Similar as #15647 but using a SQL migration as Migrate support for multi-schema is not here yet
Jolg42 added a commit that referenced this issue Oct 14, 2022
…gration

Closes #15009

Similar as #15647 but using a SQL migration as Migrate support for multi-schema is not here yet
@janpio janpio modified the milestones: 4.5.0, 4.6.0 Oct 18, 2022
@tomhoule tomhoule assigned Jolg42 and unassigned tomhoule Oct 19, 2022
Jolg42 added a commit that referenced this issue Oct 25, 2022
…gration

Closes #15009

Similar as #15647 but using a SQL migration as Migrate support for multi-schema is not here yet
Jolg42 added a commit that referenced this issue Oct 25, 2022
Jolg42 added a commit that referenced this issue Nov 7, 2022
…gration

Closes #15009

Similar as #15647 but using a SQL migration as Migrate support for multi-schema is not here yet
@janpio janpio modified the milestones: 4.6.0, 4.7.0 Nov 8, 2022
Jolg42 added a commit that referenced this issue Nov 9, 2022
…gration

Closes #15009

Similar as #15647 but using a SQL migration as Migrate support for multi-schema is not here yet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment