Coder Social home page Coder Social logo

Comments (6)

twobyte avatar twobyte commented on April 20, 2024

Here is the prisma/seed.ts file:

import { PrismaClient } from "@prisma/client";
import bcrypt from "bcryptjs";

const prisma = new PrismaClient();

async function seed() {
  const email = "[email protected]";

  // cleanup the existing database
  await prisma.user.delete({ where: { email } }).catch(() => {
    // no worries if it doesn't exist yet
  });

  const hashedPassword = await bcrypt.hash("racheliscool", 10);

  const user = await prisma.user.create({
    data: {
      email,
      password: {
        create: {
          hash: hashedPassword,
        },
      },
    },
  });

  await prisma.note.create({
    data: {
      title: "My first note",
      body: "Hello, world!",
      userId: user.id,
    },
  });

  await prisma.note.create({
    data: {
      title: "My second note",
      body: "Hello, world!",
      userId: user.id,
    },
  });

  const posts = [
    {
      slug: "my-first-post",
      title: "My First Post",
      markdown: `
  # This is my first post
  
  Isn't it great?
      `.trim(),
    },
    {
      slug: "90s-mixtape",
      title: "A Mixtape I Made Just For You",
      markdown: `
  # 90s Mixtape
  
  - I wish (Skee-Lo)
  - This Is How We Do It (Montell Jordan)
  - Everlong (Foo Fighters)
  - Ms. Jackson (Outkast)
  - Interstate Love Song (Stone Temple Pilots)
  - Killing Me Softly With His Song (Fugees, Ms. Lauryn Hill)
  - Just a Friend (Biz Markie)
  - The Man Who Sold The World (Nirvana)
  - Semi-Charmed Life (Third Eye Blind)
  - ...Baby One More Time (Britney Spears)
  - Better Man (Pearl Jam)
  - It's All Coming Back to Me Now (Céline Dion)
  - This Kiss (Faith Hill)
  - Fly Away (Lenny Kravits)
  - Scar Tissue (Red Hot Chili Peppers)
  - Santa Monica (Everclear)
  - C'mon N' Ride it (Quad City DJ's)
      `.trim(),
    },
  ];
  
  for (const post of posts) {
    await prisma.post.upsert({
      where: { slug: post.slug },
      update: post,
      create: post,
    });
  }

  console.log(`Database has been seeded. 🌱`);
}

seed()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

from examples.

MichaelDeBoey avatar MichaelDeBoey commented on April 20, 2024

@twobyte Did you create a migration first?

npx prisma migrate dev

from examples.

twobyte avatar twobyte commented on April 20, 2024

@MichaelDeBoey yes I did, it says Already in sync, no schema change or pending migration was found.

In fact, I tried the recommendations on this page yesterday before posting, but it hasn’t helped.
https://www.prisma.io/docs/guides/database/developing-with-prisma-migrate/troubleshooting-development

I see there is an update available for Prisma, will try that now 4.3.1 -> 4.4.0

from examples.

twobyte avatar twobyte commented on April 20, 2024

Still no luck using Prisma v4.4 I’m afraid. I tried npx prisma migrate reset again and still same error.

I feel like I must be missing something very simple about registering the Post property to the PrismaClient – but I cannot see what! The model is listed in prisma/schema.prisma file, where else must it be registered to be able to seed?

Or could there be an issue with my Typescript environment?

The error again:

TSError: ⨯ Unable to compile TypeScript:
prisma/seed.ts:81:18 - error TS2339: Property 'post' does not exist on type 'PrismaClient<PrismaClientOptions, never, RejectOnNotFound | RejectPerOperation | undefined>'.

81     await prisma.post.upsert({
                    ~~~~

    at createTSError (/Remix/blog-tutorial/node_modules/ts-node/src/index.ts:859:12)
    at reportTSError (/Remix/blog-tutorial/node_modules/ts-node/src/index.ts:863:19)
    at getOutput (/Remix/blog-tutorial/node_modules/ts-node/src/index.ts:1077:36)
    at Object.compile (/Remix/blog-tutorial/node_modules/ts-node/src/index.ts:1433:41)
    at Module.m._compile (/Remix/blog-tutorial/node_modules/ts-node/src/index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Object.require.extensions.<computed> [as .ts] (/Remix/blog-tutorial/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  diagnosticCodes: [ 2339 ]
}

An error occured while running the seed command:
Error: Command failed with exit code 1: ts-node --require tsconfig-paths/register prisma/seed.ts

from examples.

twobyte avatar twobyte commented on April 20, 2024

I have posted the code here:
https://github.com/twobyte/remix-tutorial
Does this code work for anyone else?

from examples.

twobyte avatar twobyte commented on April 20, 2024

Well that is the strangest thing. It looks like prisma/schema.prisma was not even saving my changes! I had to commit to the repo to see this.

Not sure if this was me being really dumb (pretty sure I saved the file many times) or an issue with VSCode not updating. In any case, this issue is resolved. Thank you for your time/attention and all your hard-work developing Remix.

from examples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.