You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
After upgrading from 2.0.2 to 2.1.0, a function that I use to seed my database fails with the error Error: No prisma client. I confirmed that initialize is called prior to this. Even moving the initialize call immediately before UserFactory.create() does not resolve it.
To Reproduce
Steps to reproduce the behavior:
Upgrade to 2.1.0
Run the Prisma migrate command to rebuild the client
Run tests
Expected behavior
It should create database records without crashing :-)
Your Schema
Sorry, I can't provide this.
Environment (please complete the following information):
Database kind - Postgres
Node.js version - 20.10.0
@prisma/client version - 5.8.1
TypeScript version - 5.2.2
Additional context
There might be some important things to know.
I perform some database operations at the beginning of each run of my tests. I am using Vite and have a globalSetup file, globalDbSetup.ts. That file is this:
import{dbConnectionHelper}from'./testDbHelper';letdbBootstrapped=false;exportdefaultasyncfunctionsetup({ provide }: {provide: (key: string,value: unknown)=>void}){if(dbBootstrapped){thrownewError('We already setup the database once!');}dbBootstrapped=true;dbConnectionHelper.setup();// This is going to cleanup the database by truncating it. It ensures we always start with a clean slate.awaitdbConnectionHelper.cleanup();constuserId=awaitdbConnectionHelper.bootstrap();if(!userId){thrownewError('ERROR: no user was created!');}provide('userId',userId);}
The dbConnectionHelper is a singleton object that is responsible for the connection. It looks like this:
import{getPrismaClient}from'@/lib/getPrismaClient';import{initialize}from'./__generated__/fabbrica';import{UserFactory}from'./factories/UserFactory';classDbConnectionHelper{connection?: ReturnType<typeofgetPrismaClient>;userId?: string;constructor(){if(!process.env.VITEST){thrownewError('ERROR: This class is not for use outside of testing!');}}getUserId(){if(!this.userId){thrownewError('ERROR: no user id has been set!');}returnthis.userId;}setup(){this.connection=getPrismaClient();initialize({prisma: this.connection});}asyncbootstrap(){if(this.userId){returnthis.userId;}constuser=awaitthis.seedDatabase();this.userId=user.id;returnthis.userId;}asynccleanup(){if(!this.connection){thrownewError('ERROR: no connection has been established!');}if(!process.env.VITEST){thrownewError('ERROR: This method is not for use outside of testing!');}console.log('TRUNCATING DATABASE TABLES');consttableNames=awaitthis.getTableNames();try{forawait(consttableNameoftableNames){awaitthis.connection.$queryRawUnsafe(`TRUNCATE TABLE "${tableName}" CASCADE`);}}catch(e){console.log('CLEANUP FAILED:');console.log(e);}}asyncgetTableNames(){if(!this.connection){thrownewError('ERROR: no connection has been established!');}if(!process.env.VITEST){thrownewError('ERROR: This method is not for use outside of testing!');}consttables=awaitthis.connection.$queryRaw`SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' `;return(tablesas{table_name: string}[]).map((table)=>table.table_name).filter((name)=>name!=='_prisma_migrations'&&!name.startsWith('pg_'));}asyncseedDatabase(){if(!this.connection){thrownewError('ERROR: no connection has been established!');}if(!process.env.VITEST){thrownewError('ERROR: This method is not for use outside of testing!');}constuser=awaitUserFactory.create();// Some more setup logic happens here. We create some related objects that are expected to exist.returnuser;}}exportconstdbConnectionHelper=newDbConnectionHelper();
The failure occurs within seedDatabase as soon as we call UserFactory.create().
I use an extende Prisma client. Calling getPrismaClient() returns this.
Hey, just circling back here, I'm still getting this with 2.1.1. I'd love to get it resolved so I can stay current. If there are any tips for troubleshooting, I'm happy to do the legwork. @aiji42 pinging you since this pertains to your work as well.
I experienced this issue. The solution was as follows:
import { PrismaClient } from '@prisma/client';
import {initialize} from '.prisma/factory';
const prisma = new PrismaClient();
initialize({ prisma }); // <-- This line is important
// the rest of your code
Describe the bug
After upgrading from 2.0.2 to 2.1.0, a function that I use to seed my database fails with the error
Error: No prisma client
. I confirmed thatinitialize
is called prior to this. Even moving theinitialize
call immediately beforeUserFactory.create()
does not resolve it.To Reproduce
Steps to reproduce the behavior:
Expected behavior
It should create database records without crashing :-)
Your Schema
Sorry, I can't provide this.
Environment (please complete the following information):
@prisma/client
version - 5.8.1Additional context
There might be some important things to know.
globalSetup
file,globalDbSetup.ts
. That file is this:The
dbConnectionHelper
is a singleton object that is responsible for the connection. It looks like this:The failure occurs within
seedDatabase
as soon as we callUserFactory.create()
.getPrismaClient()
returns this.Finally, sorry for not responding about the WIP traits! I'll review those now.
The text was updated successfully, but these errors were encountered: