0

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.

2
  • 1
    did u create the tables first. npx prisma migrate dev Commented Dec 21, 2023 at 20:36
  • @Yilmaz Yes I did Commented Dec 21, 2023 at 20:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.