notiz-dev / prisma-dbml-generator

Prisma DBML Generator
MIT License
685 stars 37 forks source link

Feature request ? do not output both the foreignKeys and related object #33

Closed RodolpheGohard closed 2 years ago

RodolpheGohard commented 2 years ago

If I take the example from the readme, I see that both the Category object in post and the category-to-post relation are represented in the output.

I think the relation-as-an-attribute object categories Category should not be represented because it duplicates what the relationship table CategoryToPost says. (In the ways traditionnal diagrams are shown, and concretely, the attribute doesn't exist in the SQL schema that would be generated)

In this example, it's a Many to many, but this applies to One-To-Many as well.

Is there a workaround to hide these properties ?

Table Post {
  id Int [pk, increment]
  title String [not null, default: '']
  content String
  published Boolean [not null, default: false]
  author User
  authorId Int
  categories Category // Should not be exported
}

Table Category {
  id Int [pk, increment]
  name String [not null]
  posts Post // should not be exported
}

Table CategoryToPost {
  categoriesId Int [ref: > Category.id]
  postsId Int [ref: > Post.id]
}
marcjulian commented 2 years ago

This is now possible with the option includeRelationFields.

Update to the latest dev release npm install -D prisma-dbml-generator@dev and add includeRelationFields with false to the dmbl generator.

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

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

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

generator dbml {
  provider = "prisma-dbml-generator"
  includeRelationFields = "false"
}

model Post {
  id         Int        @id @default(autoincrement())
  title      String     @default("")
  content    String?
  published  Boolean    @default(false)
  categories Category[]
}

model Category {
  id    Int    @id @default(autoincrement())
  name  String
  posts Post[]
}

The generated DBML Schema looks as follows:

//// ------------------------------------------------------
//// THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
//// ------------------------------------------------------

Table Post {
  id Int [pk, increment]
  title String [not null, default: '']
  content String
  published Boolean [not null, default: false]
}

Table Category {
  id Int [pk, increment]
  name String [not null]
}

Table CategoryToPost {
  categoriesId Int [ref: > Category.id]
  postsId Int [ref: > Post.id]
}
RodolpheGohard commented 2 years ago

Awesome, exactly what I needed, many thanks !