P
Prismaā€¢2w ago
fhenriquez

Issue with seeding in Next.js + Prisma + Docker + SQLite

Hi everyone, I have an application with Next.js, Prisma, Docker, and SQLite. Everything works fine in development, but when I run the Docker image and execute the npm run seed command, the data is successfully added, but it doesn't reflect in the application until I restart it. This also happens when using Prisma Studio. Has anyone experienced a similar issue? How can I resolve it? I ran the container with npm run dev and everything worked fine. Does anyone know what it is causing this behavior? And how can I make it work with npm run start? Thanks in advance #
3 Replies
Prisma AI Help
Prisma AI Helpā€¢2w ago
You selected the bot's instantaneous insights. Let me compile your answer right away! Based on the knowledge sources provided, I can offer some insights into your issue with seeding in a Next.js + Prisma + Docker + SQLite setup. However, it's important to note that the information available doesn't fully address your specific scenario, especially regarding SQLite. Nevertheless, I can provide some general guidance and potential solutions. The issue you're experiencing where seeded data doesn't reflect in the application until restart is likely related to how Prisma Client is initialized and how it interacts with your database in a Docker environment. Here are a few potential reasons and solutions to consider: 1. Prisma Client Generation: Ensure that you're generating the Prisma Client correctly in your Docker setup. As mentioned in the Unable to use @prisma/client with NextJS + Docker discussion, you should generate the Prisma Client during the build stage of your Dockerfile:
# Generate Prisma client
RUN npx prisma generate --schema=./prisma/schema.prisma

# Generate Prisma client
RUN npx prisma generate --schema=./prisma/schema.prisma

2. Database Connection: Make sure your application is connecting to the correct database instance. In a Docker environment, this can sometimes be tricky. Double-check your database connection string and ensure it's correctly set in your environment variables. 3. Prisma Client Instantiation: In your Next.js application, make sure you're instantiating Prisma Client correctly. You might want to use a singleton pattern to avoid creating multiple instances of Prisma Client. Here's an example:
import { PrismaClient } from '@prisma/client'

let prisma: PrismaClient

if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient()
} else {
if (!global.prisma) {
global.prisma = new PrismaClient()
}
prisma = global.prisma
}

export default prisma

import { PrismaClient } from '@prisma/client'

let prisma: PrismaClient

if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient()
} else {
if (!global.prisma) {
global.prisma = new PrismaClient()
}
prisma = global.prisma
}

export default prisma

4. Docker Volume: Ensure that you're using a Docker volume to persist your SQLite database file. If you're not, the database might be recreated each time you restart the container, which could explain why you need to reseed. 5. Hot Reloading: The fact that it works with npm run dev but not with npm run start suggests that there might be an issue with how your application is built or how it handles database connections in production mode. You might need to implement a mechanism to reconnect or refresh the database connection when new data is seeded. Unfortunately, the provided knowledge sources don't contain specific information about using SQLite with Prisma in a Docker environment. Most of the examples and issues discussed relate to PostgreSQL. If these suggestions don't resolve your issue, you might want to consider: 1. Switching to a different database like PostgreSQL, which is more commonly used with Prisma and Docker, and has better support. 2. Implementing a custom solution to refresh your application's database connection after seeding. 3. Reaching out to the Prisma community with a more detailed description of your setup, including your Dockerfile, database schema, and seeding script. Remember to always check the Prisma documentation for the most up-to-date information and best practices when working with Prisma, especially in Docker environments. šŸ¤” Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
fhenriquez
fhenriquezOPā€¢2w ago
COPY package*.json ./ COPY ./prisma prisma ENV DATABASE_URL="file:./dev.db" ENV NODE_ENV="production" # # init prisma RUN npx prisma generate --schema=./prisma/schema.prisma RUN npx prisma migrate deploy # install dependencies RUN npm install # copy the rest of the files to the working directory COPY . . EXPOSE 4444 EXPOSE 5555 CMD npm run build && npm run start client: import { PrismaClient } from '@prisma/client' const globalForPrisma = globalThis as unknown as { prisma: PrismaClient } export const prisma = globalForPrisma.prisma || new PrismaClient() if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma export default prisma does not work
Nurul
Nurulā€¢7d ago
Hey šŸ‘‹ If I understand correctly, the seeding process gets completed successfully (You don't get any errors) but you don't see the data until you restart your application. Are you able to see the data in your database? Try connecting to the database via a GUI tool after seeding and check if the data exists?

Did you find this page helpful?