SDL generation bug with directus tables

Hi.
I’m managing my Postgres database with directus as well as prisma and redwood.
the procedure resulting in the problem is as follows:

  1. directus and prisma are in sync and everybody is happy.
  2. I create a new table (Article) with directus (which has a relation to the directus files table directus_files )
  3. I run yarn rw prisma db pull to get the newly created table in prisma.schema. it runs successfully :smile:
model Article {
  id              Int      @id @default(autoincrement())
  title           String   @db.VarChar(255)
  image           String?  @db.Uuid
  body            String
  status          String   @default("DRAFT")
  original_author String   @db.Uuid
  creation_date   DateTime @db.Timestamptz(6)
  latest_author   String   @db.Uuid
  date            DateTime @db.Timestamptz(6)

  directus_files                                         directus_files? @relation(fields: [image], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "article_image_foreign")
  directus_users_Article_latest_authorTodirectus_users   directus_users  @relation("Article_latest_authorTodirectus_users", fields: [latest_author], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "article_latest_author_foreign")
  directus_users_Article_original_authorTodirectus_users directus_users  @relation("Article_original_authorTodirectus_users", fields: [original_author], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "article_original_author_foreign")
}
  1. I run yarn rw g sdl Article to create graphql type and resolvers for my new table and get :
yarn rw g sdl Article
  × Generating SDL files...
    → No schema definition found for `directus_files` in schema.prisma file
    Generating types ...
No schema definition found for `directus_files` in schema.prisma file

but directus_files is clearly present in the schema.prisma file :

model directus_files {
  id                                                                    String              @id @db.Uuid
  storage                                                               String              @db.VarChar(255)
  filename_disk                                                         String?             @db.VarChar(255)
  filename_download                                                     String              @db.VarChar(255)
  title                                                                 String?             @db.VarChar(255)
  type                                                                  String?             @db.VarChar(255)
  folder                                                                String?             @db.Uuid
  uploaded_by                                                           String?             @db.Uuid
  uploaded_on                                                           DateTime            @default(now()) @db.Timestamptz(6)
  modified_by                                                           String?             @db.Uuid
  modified_on                                                           DateTime            @default(now()) @db.Timestamptz(6)
  charset                                                               String?             @db.VarChar(50)
  filesize                                                              BigInt?
  width                                                                 Int?
  height                                                                Int?
  duration                                                              Int?
  embed                                                                 String?             @db.VarChar(200)
  description                                                           String?
  location                                                              String?
  tags                                                                  String?
  metadata                                                              Json?               @db.Json
  directus_folders                                                      directus_folders?   @relation(fields: [folder], references: [id], onUpdate: NoAction, map: "directus_files_folder_foreign")
  directus_users_directus_files_modified_byTodirectus_users             directus_users?     @relation("directus_files_modified_byTodirectus_users", fields: [modified_by], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "directus_files_modified_by_foreign")
  directus_users_directus_files_uploaded_byTodirectus_users             directus_users?     @relation("directus_files_uploaded_byTodirectus_users", fields: [uploaded_by], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "directus_files_uploaded_by_foreign")
  Article                                                               Article[]
  Content                                                               Content[]
  directus_settings_directus_filesTodirectus_settings_project_logo      directus_settings[] @relation("directus_filesTodirectus_settings_project_logo")
  directus_settings_directus_filesTodirectus_settings_public_background directus_settings[] @relation("directus_filesTodirectus_settings_public_background")
  directus_settings_directus_filesTodirectus_settings_public_foreground directus_settings[] @relation("directus_filesTodirectus_settings_public_foreground")
}

Is anybody else having this issue too?

Have you tried commenting out the relationships between them, generating them individually. Then commenting back on the relationships and then doing a generate sdl —force ?

Can you explain how this works a bit further, @dthyresson? Aren’t the previous versions of the generated files blown away by the next call to generate? I’m having a similar issue with my setup where i run scaffold and the prisma barfs on relationships in an asynchronous way. It will sort of randomly pick one of the many relationships to complain about being missing. I thought it was a race condition until I saw your comment on Alex’s question and again in this thread suggesting the relationships be removed and then run later. Maybe a quick write up about how this should be done would be in order since it seems like this is a gotcha for anyone pulling from an old crusty db schema filled with problematic tables. I’d write something up as a first draft if i knew what was going on that wasn’t working for prisma.

Schema is required to build SDL which is required to build Types. Building the dependent tables/SDLs doesn’t seem to throw. These Prisma relationships are design in such a way that only one of the tables owns the referential definition. That order seems to matter to the generator. I suppose disabling the references in the case where there are multiple references helps to ensure you can generate the dependencies one at a time from a valid schema, then the references will exist for the main table to be yarn rw g sdl MODELNAME --force created which overwrites the old file without the references in it.

So that’s done, but I end up with GraphQL type check errors in api/src/graphql/*.sdl.ts wherever there is a defined relationship. “Unknown type “User”.” in regards to a definition like:

  type UserSession {
    id: Int!
    user_id: Int!
    users: User!
  }

That still seems problematic.