juju
juju

Reputation: 301

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:

"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.

Upvotes: 0

Views: 741

Answers (1)

Hoopra
Hoopra

Reputation: 853

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.

Upvotes: 1

Related Questions