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

Prisma Parser - Support @map #556

Open
MH4GF opened this issue Jan 24, 2025 · 1 comment
Open

Prisma Parser - Support @map #556

MH4GF opened this issue Jan 24, 2025 · 1 comment
Labels

Comments

@MH4GF
Copy link
Member

MH4GF commented Jan 24, 2025

Task description

model User {
  id String @default(auto()) @map("_id") @db.ObjectId
}

ref: https://www.prisma.io/docs/orm/prisma-schema/data-model/models#mapping-model-names-to-tables-or-collections

Field names and actual column names can be set separately and need to be supported.
For ERD tools, we want to use properties that match the actual table.

@MH4GF MH4GF added Area: Parser good first issue Good for newcomers labels Jan 24, 2025
@MH4GF
Copy link
Member Author

MH4GF commented Jan 24, 2025

📝 I'm noticing that a different approach seems to be needed, so here's a note.

Suppose we have a schema like the following:

        model users {
          id Int @id @default(autoincrement())
          posts posts[].
        }

        model posts {
          id Int @id @default(autoincrement())
          user users @relation(fields: [user_id], references: [id])
          user_id Int @map(“raw_user_id”))
        }

Since raw_user_id specified by @map is the column name of the real table, we want to use this for DBStructure.

field obj: user_id
{
  name: 'user_id', dbName: 'raw_user_id
  dbName: 'raw_user_id', // we can get it from here
  kind: 'scalar',
  isList: false, isRequired: true, isRequired: true, isRequired: false
  isRequired: true, isUnique: false, isSubject: false, isSubject: false
  isUnique: false, isId: false, isScalar: true, isList: false, isRequired: true, isScalar: false
  isId: false, isReadOnly: true, isReadOnly: true
  
  hasDefaultValue: false, isDefaultValue: false, isDefaultValue: false, isDefaultValue: false, isDefaultValue: false
  type: 'Int', nativeType: null, nativeType: null, nativeType: null, nativeType: null
  
  isGenerated: false, isUpdatedAt: false
  isUpdatedAt: false
}

And the post.user_id relation would look like this.

{
  name: 'user', ``type: 'object', ``s
  
  isList: false, isRequired: true, isRequired: true, isRequired: false, isRequired: true, isRequired: false
  
  
  isId: false, isReadOnly: false, isReadOnly: false, isReadOnly: false, isReadOnly: false
  isReadOnly: false, isRequired: true, isUnique: false, isId: false, isReadOnly: false, isDefaultValue: false, isDefaultValue: false
  hasDefaultValue: false, type: 'users', isList: false, isRequired: true, isUnique: false, isId: false, isReadOnly: false, isDefaultValue: false
  
  
  
  relationFromFields: [ 'user_id' ], relationToFields: [ 'id
  
  isGenerated: false, isUpdatedAt: false
  isUpdatedAt: false
}

The problem is that relationFromFields references a user_id column.
If you use raw_user_id for DBStructure, it will not be associated correctly.
You will need to do handling for the association separately.

The @map names are also used for index naming, but they cannot be obtained directly from the index obj, so handling is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant