Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Prisma not creating records

I’m trying to run a simple seed function to generate some test data for an application. The app is built on NextJS 14 which utilizes server actions. The function is being called and no errors are being thrown, but there are new rows generated when I check the database.

Client code:

import { useEffect } from "react"
import { seedGuests } from "./actions"

export default function Welcome() {
  useEffect(() => {
    seedGuests()
  }, [])

Actions code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"use server"
import { prisma } from "@/prisma/client"

export async function seedGuests() {
  try {
    prisma.guest.create({
      data: {
        name: "John Doe",
        can_bring_guest: true,
      },
    })
    prisma.guest.create({
      data: {
        name: "Billy Sparks",
        can_bring_guest: false,
      },
    })
    console.log("guests created")
  } catch (e) {
    console.log(e)
  }
}

prisma/client.ts code:

import { PrismaClient } from "@prisma/client"
export const prisma = new PrismaClient()

prisma.schema:

model guest {
  id              Int      @id @default(autoincrement())
  name            String
  attending       Boolean?
  guest_count     Int?
  can_bring_guest Boolean
}

I see the "guests created" console log in the terminal and no errors are logged. However, no new rows are created in the database.

>Solution :

You need to await prisma queries:

    await prisma.guest.create({
      data: {
        name: "John Doe",
        can_bring_guest: true,
      },
    })
    await prisma.guest.create({
      data: {
        name: "Billy Sparks",
        can_bring_guest: false,
      },
    })

Your code starts the executions, then logs the message, but doesn’t wait for the seed data to be inserted.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading