Hi There. I’m about to build a new API and I would...
# orm-help
d
Hi There. I’m about to build a new API and I would like to try Prisma this time. However, the database already exists with 100+ tables in it. Is there a way to auto-generate the classes for the DB entities?
1
d
Thanks, I will take a look at that!
n
Welcome to our community! 🙌 Do let us know in case you run into any issues in introspecting your database.
d
@Nurul thanks, and I will 😄
@Nurul so I rang
prisma db pull
, created a new data service in
nestjs
Copy code
import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class DataService extends PrismaClient implements OnModuleInit {
  async onModuleInit() {
    await this.$connect();
  }

  async enableShutdownHooks(app: INestApplication) {
    this.$on('beforeExit', async () => {
      await app.close();
    });
  }
}
Copy code
[Nest] 34568  - 08/08/2022, 12:27:54   ERROR [ExceptionHandler] @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
In case this error is unexpected for you, please report it in <https://github.com/prisma/prisma/issues>
Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
Am I still supposed to run
prisma generate
when I used the introspect tool?
n
Yes, you need to execute
prisma generate
command every time there is changes to the schema file.
d
Aaa I see. So Prisma generates a huge
schema
file. However it doesn’t seem like it is possible to have two fields with the same name in the database?
Copy code
error: Field "Year" is already defined on model "tblibsclaims".
  -->  schema.prisma:704
   | 
703 |   Year      Int    @default(0) @map("1Year")
704 |   Year      Int    @default(0) @map("3Year")
   | 
error: Field "Years" is already defined on model "tblibsconvictions".
  -->  schema.prisma:718
   | 
717 |   Years          Int?    @default(100) @map("3Years")
718 |   Years          Int?    @default(100) @map("5Years")
And thanks for being so friendly!
🙌 1
n
That's correct, there can't be two fields with the same name.
d
Oooh, it is because the one who created the db named the fields 1Year, 3Year and so on…..
j
You should be able to edit your schema and change the definition to suit. example:
Copy code
703 |   Year1      Int    @default(0) @map("1Year")
704 |   Year3      Int    @default(0) @map("3Year")
Note that in javascript identifiers can't start with a number, so
1Year
won't be a valid name.
d
I figured that out 🙂 thanks.
🙌 1