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

should use mapped column and table names #18

Open
keenahn opened this issue Aug 27, 2021 · 12 comments
Open

should use mapped column and table names #18

keenahn opened this issue Aug 27, 2021 · 12 comments

Comments

@keenahn
Copy link

keenahn commented Aug 27, 2021

since this is supposed to be the actual database diagram, shouldn't the generated dbml use the mapped table and column names from @map and @@map ?

@marcjulian
Copy link
Member

@keenahn good idea. I think I prefer to haven an option to use the names either from the models/fields or the names for @map and @@map. What do you think?

@Katli95
Copy link

Katli95 commented Dec 21, 2021

Yeah having the option at least would be cool! I think I'd prefer to export the actual Database Schema for the diagram, even though the Prisma client surfaces another API. Another thing to look at would be to include the db type attributes such as @db.VarChar(5000).

But overall, awesome little utility, I'm starting to integrate it into my workflow!

@marcjulian
Copy link
Member

marcjulian commented Mar 18, 2022

For the following schema with @map, @@map and @db., only @@map is surfaced in the DMMF schema from Prisma. Thus, for now only the dbName can be mapped to the value specified in @@map.

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now()) @db.DateTime(0)
  updatedAt DateTime @updatedAt @map("updated_at")
  email     String   @unique
  name      String?  @db.VarChar(255)

  @@map("user")
}
DMMF
{
  "enums": [],
  "models": [
    {
      "name": "User",
      "dbName": "user", // 👈 @@map
      "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": "createdAt",
          "kind": "scalar",
          "isList": false,
          "isRequired": true,
          "isUnique": false,
          "isId": false,
          "isReadOnly": false,
          "type": "DateTime",
          "hasDefaultValue": true,
          "default": { "name": "now", "args": [] },
          "isGenerated": false,
          "isUpdatedAt": false
        },
        {
          "name": "updatedAt",
          "kind": "scalar",
          "isList": false,
          "isRequired": true,
          "isUnique": false,
          "isId": false,
          "isReadOnly": false,
          "type": "DateTime",
          "hasDefaultValue": false,
          "isGenerated": false,
          "isUpdatedAt": true
        },
        {
          "name": "email",
          "kind": "scalar",
          "isList": false,
          "isRequired": true,
          "isUnique": true,
          "isId": false,
          "isReadOnly": false,
          "type": "String",
          "hasDefaultValue": false,
          "isGenerated": false,
          "isUpdatedAt": false
        },
        {
          "name": "name",
          "kind": "scalar",
          "isList": false,
          "isRequired": false,
          "isUnique": false,
          "isId": false,
          "isReadOnly": false,
          "type": "String",
          "hasDefaultValue": false,
          "isGenerated": false,
          "isUpdatedAt": false
        }
      ],
      "isGenerated": false,
      "primaryKey": null,
      "uniqueFields": [],
      "uniqueIndexes": []
    }
  ],
  "types": []
}

@marcjulian
Copy link
Member

marcjulian commented Mar 18, 2022

I added mapToDbSchema option for you to test out. You can test it with the latest dev release 0.9.0-dev.1

generator dbml {
  provider    = "node ./dist/generator.js"
  mapToDbSchema = "true" // 👈  enable mapToDbSchema with the new option
}

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now()) 
  updatedAt DateTime @updatedAt
  email     String   @unique
  name      String? 

  @@map("user") // 👈  this will be used in the dbml schema
}

@dominikjasek
Copy link

Could anybody confirm that release 0.9.0 fixed this? It is still not working for me personally.

@marcjulian
Copy link
Member

marcjulian commented May 5, 2022

@dominikjasek Did you add mapToDbSchema to the dbml generator as this is not enabled by default? Otherwise do you have a prisma.schema to reproduce?

@dominikjasek
Copy link

dominikjasek commented May 5, 2022

It works on table names but not on relations, when using @@map('users') on User Model etc.
Expected: Ref: answers.userId > users.id
Got: Ref: Answer.userId > User.id

@marcjulian
Copy link
Member

Ah good point. I might have missed that! I'll look into it soon

@marcjulian
Copy link
Member

@dominikjasek Please update to 0.9.1 and test it again.

@dominikjasek
Copy link

Thanks! I confirm it is working.

@hotrungnhan
Copy link

hallo guy i have
field name wasn't mapped right to the dbml.
prisma-dbml-generator: 0.9.1
prisma: 4.0.1

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

generator dbml {
  provider              = "node node_modules/prisma-dbml-generator/dist/generator.js"
  mapToDbSchema         = true
  includeRelationFields = false
  projectName           = "Backend DB"
}

generator docs {
  provider = "node node_modules/prisma-docs-generator"
}

generator nestgraphql {
  provider = "node node_modules/prisma-nestjs-graphql"
  output   = "./@generated/graphql"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id          Int    @id @default(autoincrement())
  email       String @unique
  displayName String @map("display_name")
  password    String

  @@map("user")
}

model Movies {
  id          Int          @id @default(autoincrement())
  title       String?
  code        String?
  description Json?
  thumbnail   String
  seo         Seo?         @relation(fields: [seoId], references: [id])
  seoId       Int?         @map("seo_id")
  movieVideo  MovieVideo[]
  video       Video[]

  @@map("movies")
}

model MovieVideo {
  movieId Int    @map("movie_id")
  videoId Int    @map("video_id")
  movie   Movies @relation(fields: [movieId], references: [id])
  video   Video  @relation(fields: [videoId], references: [id])

  @@id([movieId, videoId])
  @@map("movie_video")
}

model Seo {
  id          Int      @id @default(autoincrement())
  title       String?
  description String?
  keywords    String[]
  slug        String   @default(dbgenerated("'<(10,4),11>'::circle"))
  thumbnail   String
  movies      Movies[]

  @@map("seo")
}

model VideoProvider {
  id       Int     @id @default(autoincrement())
  provider String
  video    Video[]

  @@map("video_provider")
}

model Video {
  id         Int           @id @default(autoincrement())
  url        String
  providerId Int           @map("provider_id")
  provider   VideoProvider @relation(fields: [providerId], references: [id])
  movieVideo MovieVideo[]
  movies     Movies?       @relation(fields: [moviesId], references: [id])
  moviesId   Int?          @map("movies_id")

  @@map("video")
}

model VideoCategory {
  id   Int     @id @default(autoincrement())
  name String?

  @@map("video_category")
}

model Actor {
  id     Int      @id @default(autoincrement())
  name   String?
  images String[]

  @@map("actor")
}

@erlanggadewa
Copy link

erlanggadewa commented Jan 18, 2024

For the following schema with @map, @@map and @db., only @@map is surfaced in the DMMF schema from Prisma. Thus, for now only the dbName can be mapped to the value specified in @@map.

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now()) @db.DateTime(0)
  updatedAt DateTime @updatedAt @map("updated_at")
  email     String   @unique
  name      String?  @db.VarChar(255)

  @@map("user")
}

DMMF

{
  "enums": [],
  "models": [
    {
      "name": "User",
      "dbName": "user", // 👈 @@map
      "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": "createdAt",
          "kind": "scalar",
          "isList": false,
          "isRequired": true,
          "isUnique": false,
          "isId": false,
          "isReadOnly": false,
          "type": "DateTime",
          "hasDefaultValue": true,
          "default": { "name": "now", "args": [] },
          "isGenerated": false,
          "isUpdatedAt": false
        },
        {
          "name": "updatedAt",
          "kind": "scalar",
          "isList": false,
          "isRequired": true,
          "isUnique": false,
          "isId": false,
          "isReadOnly": false,
          "type": "DateTime",
          "hasDefaultValue": false,
          "isGenerated": false,
          "isUpdatedAt": true
        },
        {
          "name": "email",
          "kind": "scalar",
          "isList": false,
          "isRequired": true,
          "isUnique": true,
          "isId": false,
          "isReadOnly": false,
          "type": "String",
          "hasDefaultValue": false,
          "isGenerated": false,
          "isUpdatedAt": false
        },
        {
          "name": "name",
          "kind": "scalar",
          "isList": false,
          "isRequired": false,
          "isUnique": false,
          "isId": false,
          "isReadOnly": false,
          "type": "String",
          "hasDefaultValue": false,
          "isGenerated": false,
          "isUpdatedAt": false
        }
      ],
      "isGenerated": false,
      "primaryKey": null,
      "uniqueFields": [],
      "uniqueIndexes": []
    }
  ],
  "types": []
}

can you create a new option to make name field to snake case ? Because a lot of user using snake case in @Map notation
exampe -> updatedAt become updated_at ?

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

No branches or pull requests

6 participants