Problem with generate and generate --sql
HI,
I have multiple problems with
npx prisma generate .
I've updated to the newest version and tried to use typedSql .
1. When I generate prisma client, I get dozens of errors in my existing code. As I am importing PrismaClient from node_modules and not generated folded ( I want to keep it that way). After some digging I see that PrismaClient is generated as the type of any and everything breaks. After around 50 retries of running generate command, the PrismaClient was generated correctly...
2. I decided to rollback to previous version (6.13.0), but npx prisma generate is running with v6.19.0. Is there any cache or something?
3. What should I do in the pipeline when it comes to --sql? Should I commit generated folder or run --sql command in the pipeline? My workflow will spin-up postgres to execute int test. So when I use "build": "npx prisma generate && tsc -p . && tscp" the first error is dispalyed in the logs. If I try "build": "npx prisma generate && npx prisma generate --sql && tsc -p . && tscp" , I get the error saying that the database is not running on localhost..
4 Replies
You chose to debug with a human. They'll tinker with your query soon. If you get curious meanwhile, hop into
#ask-ai for a quick spin!Update:
If I don't specify output in the prisma.schema file, the prisma client is generated correctly. However, if I set output location, and try to import prisma client from generated file, It's not the same type as prisma client from node_modules..
Hey!
1. How are you generating Prisma Client? I assume you have configured an output path. Do you get errors when you generate Prisma Client at custom path or when you generate it in node_modules?
2. Did you try deleting all node_modules and see if it still uses 6.19.0 version
3. The generated folder shouldn't be committed.
https://www.prisma.io/docs/orm/prisma-schema/overview/generators#3-exclude-the-generated-directory-from-version-control
Generators (Reference) | Prisma Documentation
Generators in your Prisma schema specify what assets are generated when the
prisma generate command is invoked. This page explains how to configure generators.Hey @Nurul (Prisma)
A bit of context: As I've mentioned, I'm importing prisma client from which requires output path specified. (not sure if output is required)
1. If I set output path and run
@prisma/client. I didn't have output path, and now I want to use typedSql, npx prisma generate :
a) PrismaClient from node_modules is generated as PrismaClient: any.
b) PrismaClient from generated has different type than my previous prisma client
2. I tried to delete node_module, --force npm, clear cache.. Still generates with wrong version.
The only way that worked for me, was to set output path, generate sql (npx prisma generate --sql), then removed output path and run npx prisma generate again..
import { Prisma, PrismaClient } from '@prisma/client';
import { appConfig } from '../environment';
declare global {
namespace NodeJS {
interface Global {
cachedPrisma?: PrismaClient;
}
}
}
interface CustomNodeJsGlobal extends NodeJS.Global {
prisma?: PrismaClient<Prisma.PrismaClientOptions, 'query' | 'info'>;
}
declare const global: CustomNodeJsGlobal;
const prisma =
global.prisma ||
new PrismaClient({
log: [
{ emit: 'event', level: 'query' },
{ emit: 'event', level: 'info' },
],
});
const trackerOff = ['production', 'staging', 'local', 'test'].includes(appConfig.getEnvironment());
prisma.$on('info', (e) => {
if (trackerOff) return;
const { message, target, timestamp } = e;
console.log(${timestamp} - ${message});
});
prisma.$on('query', (e) => {
if (trackerOff) return;
console.log(' ');
console.log('Timestamp: ' + e.timestamp);
console.log('Query: ' + e.query);
console.log('Params: ' + e.params);
console.log('Duration: ' + e.duration + 'ms');
});
if (appConfig.getEnvironment() === 'local') global.prisma = prisma;
export default prisma;