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

Include @@index and @map information in getDMMF #3998

Open
Tracked by #18561
gogoout opened this issue Oct 22, 2020 · 13 comments
Open
Tracked by #18561

Include @@index and @map information in getDMMF #3998

gogoout opened this issue Oct 22, 2020 · 13 comments
Labels
kind/feature A request for a new feature. team/client Issue for team Client. tech/engines Issue for tech Engines. topic: dmmf

Comments

@gogoout
Copy link

gogoout commented Oct 22, 2020

Problem

Given schema

model UserEmailHistory {
  id               Int       @id @default(autoincrement())
  user_alias       String    @map("alias")
  send_count       Int
  historyable_id   Int?
  historyable_type String?
  user_id          Int
  created_at       DateTime?
  updated_at       DateTime? @updatedAt

  @@index([user_id], name: "user_email_history_user_id_foreign")
  @@map("user_email_history")
}

using getDMMF will dump as

{
  "name": "UserEmailHistory",
  "isEmbedded": false,
  "dbName": "user_email_history",
  "fields": [
    {
      "name": "id",
      "kind": "scalar",
      "isList": false,
      "isRequired": true,
      "isUnique": false,
      "isId": true,
      "isReadOnly": false,
      "type": "Int",
      "hasDefaultValue": true,
      "default": {
        "name": "autoincrement",
        "args": []
      },
      "isGenerated": false,
      "isUpdatedAt": false
    },
    {
      "name": "user_alias",
      "kind": "scalar",
      "isList": false,
      "isRequired": true,
      "isUnique": false,
      "isId": false,
      "isReadOnly": false,
      "type": "String",
      "hasDefaultValue": false,
      "isGenerated": false,
      "isUpdatedAt": false
    },
    {
      "name": "send_count",
      "kind": "scalar",
      "isList": false,
      "isRequired": true,
      "isUnique": false,
      "isId": false,
      "isReadOnly": false,
      "type": "Int",
      "hasDefaultValue": false,
      "isGenerated": false,
      "isUpdatedAt": false
    },
    {
      "name": "historyable_id",
      "kind": "scalar",
      "isList": false,
      "isRequired": false,
      "isUnique": false,
      "isId": false,
      "isReadOnly": false,
      "type": "Int",
      "hasDefaultValue": false,
      "isGenerated": false,
      "isUpdatedAt": false
    },
    {
      "name": "historyable_type",
      "kind": "scalar",
      "isList": false,
      "isRequired": false,
      "isUnique": false,
      "isId": false,
      "isReadOnly": false,
      "type": "String",
      "hasDefaultValue": false,
      "isGenerated": false,
      "isUpdatedAt": false
    },
    {
      "name": "user_id",
      "kind": "scalar",
      "isList": false,
      "isRequired": true,
      "isUnique": false,
      "isId": false,
      "isReadOnly": false,
      "type": "Int",
      "hasDefaultValue": false,
      "isGenerated": false,
      "isUpdatedAt": false
    },
    {
      "name": "created_at",
      "kind": "scalar",
      "isList": false,
      "isRequired": false,
      "isUnique": false,
      "isId": false,
      "isReadOnly": false,
      "type": "DateTime",
      "hasDefaultValue": false,
      "isGenerated": false,
      "isUpdatedAt": false
    },
    {
      "name": "updated_at",
      "kind": "scalar",
      "isList": false,
      "isRequired": false,
      "isUnique": false,
      "isId": false,
      "isReadOnly": false,
      "type": "DateTime",
      "hasDefaultValue": false,
      "isGenerated": false,
      "isUpdatedAt": true
    }
  ],
  "isGenerated": false,
  "idFields": [],
  "uniqueFields": [],
  "uniqueIndexes": []
}

the @@index and @map information are both missing, this make printing dmmf back to schema impossible.

Suggested solution

Return indexes in Model field just like uniqueFields and uniqueIndexes.
add colName or fieldName to show the original db columnName just like dbName.

@janpio janpio added kind/feature A request for a new feature. team/product labels Oct 22, 2020
@janpio
Copy link
Member

janpio commented Oct 22, 2020

Why would that information be helpful for you, what problem does it solve @gogoout?

@gogoout
Copy link
Author

gogoout commented Oct 22, 2020

Hi, thanks for the quick response. My use case is pretty much like this issue here: IBM/prisma-schema-transformer#8
So we use DMMF act kind like a parser and then do some string transform on the model name and field name. That all went well until we want to print the DMMF back to schema. Like the issue I linked, the index will be removed due to lack of index information.

Current workaround is simply run another npx prisma introspect which will bring back all the index, but it would be nice to have the index info at first place.

@gogoout
Copy link
Author

gogoout commented Oct 28, 2020

Another thing I just noticed is missing is the @map on the column. This makes the incremental transform (or simply rerun the transform) not possible. Do I need to open another issue for this or just update this one will be enough?

@pantharshit00
Copy link
Contributor

@gogoout If that is very related to this issue, just update the description, otherwise open a new issue.

@janpio janpio changed the title Include @@Index information in getDMMF Include @@Index information in getDMMF Oct 28, 2020
@gogoout gogoout changed the title Include @@Index information in getDMMF Include @@Index and @map information in getDMMF Oct 28, 2020
@janpio janpio added topic: dmmf team/client Issue for team Client. tech/engines Issue for tech Engines. labels Apr 9, 2021
@sabinadams
Copy link

sabinadams commented Dec 24, 2021

This would be an awesome enhancement that would allow for some nice tooling and transforms to be built.

I'm needing the @Map attribute for my library aurora that stitches multiple schema files together.

Currently, the @Map attributes on fields are being omitted in the resulting schema file because that data isn't present in the getDMMF() output. The @@Map and the @Map for enums are showing up. There is an optional key in the Model.Field type for dbName that just isn't getting populated.

@RobertCraigie
Copy link

I also have a use case for this: RobertCraigie/prisma-client-py#249

Prisma Client Python provides helper methods for casting raw queries back to the standard model classes, e.g.

query = '''
    SELECT *
    FROM User
    WHERE User.id = ?
'''
user = await client.query_first(query, user.id, model=User)
# or just
user = await User.prisma().query_first(query, user.id)

This means that the returned records from raw queries are also type safe!

However this does not work for fields that use @map as the data returned from the raw query will now refer to a different field name, e.g.

model User {
  id      String   @id @default(cuid())
  name    String   @map("username")
}

This causes a validation error at runtime (showcased in the aforementioned issue). Having access to the mapped name would allow me to easily solve this bug.

@gogoout
Copy link
Author

gogoout commented Jan 27, 2022

I recommend using https://github.com/MrLeebo/prisma-ast for now until Prisma team complete their dmmf info.
Ast should give you everything you need from the schema, however, it maybe a bit harder to extract the info from it, you may need to create an abstraction layer on top of it to represent what's in the dmmf.

@dimaip
Copy link

dimaip commented Jul 1, 2022

We also need @map information on fields quite desperately! See my usecase #14087

Shouldn't be a difficult things to fix, right? And would open so many possibilities for better tooling. 🙏

@dimaip
Copy link

dimaip commented Jul 5, 2022

For the time being I had to jump through terrible hoops doing prisma.schema pre-processing and extracting all the needed attributes (@map and @db.ObjectId) into the field's documentation, and accessing it via DMMF from there.

But yeah, would be actually great to see all Prisma schema attributes accessible in DMMF.

@RobertCraigie
Copy link

I have another use case for this.

I cannot currently support DateTime fields in the Python client that are marked as @db.Date because the client generates datetime.datetime types which results in failing runtime validation because values such as 2022-01-20 are not a valid datetime format. RobertCraigie/prisma-client-py#657

@valtyr
Copy link

valtyr commented Mar 20, 2023

Hi there. I'm working on a custom generator that generates Kysely types from a Prisma schema. As of right now there's no way for me to support @map statements, since they're not returned with the DMMF. This means that users of my library that use @map end up with broken types. The only way for me to support this would be to parse the Prisma schema on my own, which although doable seems redundant. I think it would be a great improvement to pass this info along with the DMMF if only for consistency's sake. Here's a link to the issue on my repo: valtyr/prisma-kysely#4

Thanks 😄

@janpio janpio mentioned this issue Mar 29, 2023
25 tasks
@jamiter
Copy link

jamiter commented Jun 5, 2023

I wanted to retrieve the indexes to set them manually, because I can't use db push with MongoDB (because I have a couple of views in my database, see #18424).

Sadly not available, sigh... Blocked again.

@janpio janpio changed the title Include @@index and @map information in getDMMF Include @@index, @@unique and @map information in getDMMF Aug 31, 2023
@janpio janpio changed the title Include @@index, @@unique and @map information in getDMMF Include @@index and @map information in getDMMF Aug 31, 2023
@dvins
Copy link

dvins commented Mar 22, 2024

This is similarly related to #17754 wherein DMMF does not distinguish models or views, either via a distinct collection or at least through an attribute.

These sorts of issues, especially missing metadata in the DMMF, continue to frustrate work arounds and the fostering of a stronger ecosystem for Prisma.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. tech/engines Issue for tech Engines. topic: dmmf
Projects
None yet
Development

No branches or pull requests

9 participants