prisma: Seeding not working in 2.23.0 in Windows

Bug description

When seeding the database, prisma/seed.ts doesn’t seem to be getting called, despite saying that it seeded my database. I tried wrapping lines 30-37 in an export const seed = () => { ... }; and also with export default function() { ... }. In all three cases, none of the console.log() calls are logged, and the database is not updated. I also know that it knows prisma/seed.ts exists, as when I delete the file it errors. Reproduction steps are mostly just following the code at Start from scratch | TypeScript & MySQL | Prisma Docs and prisma-examples/seed.ts at latest · prisma/prisma-examples.

(Note: when switching the version of prisma and @prisma/client back to 2.22.1, bug no longer occurs and functionality is normal. Breaks again when switching back to 2.23.0)

How to reproduce

  1. Generate a new nodejs project with yarn init
  2. Run yarn add @prisma/client and yarn add --dev @types/node prisma ts-node typescript
  3. Run prisma init
  4. Set DATABASE_URL in .env file
  5. Create tsconfig.json
  6. Run prisma migrate dev --name init
  7. Create seed.ts in folder with schema.prisma
  8. Run prisma db seed --preview-feature
  9. See output (no console.logs are called, database doesn’t update)

Expected behavior

No response

Prisma information

.env:

DATABASE_URL="mysql://xxx:yyy@zzz/prismabugrepo"
DEBUG="*"

tsconfig.json:

{
  "compilerOptions": {
    "sourceMap": true,
    "outDir": "dist",
    "strict": true,
    "lib": ["esnext"],
    "esModuleInterop": true
  }
}

prisma/schema.prisma:

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id          String   @id @default(uuid())
  joinedAt    DateTime @default(now())
  username    String   @unique
  displayName String?
}

prisma/seed.ts:

import {Prisma, PrismaClient} from "@prisma/client";

console.log("Top of script");

const prisma = new PrismaClient({
  log: ["query", "info", "warn", "error"]
});

const main = async () => {
  console.log("Start seeding...");

  const userData: Prisma.UserCreateInput[] = [
    {
      username: "user123",
      displayName: "User One Two Three"
    }
  ];

  for (const u of userData) {
    const user = await prisma.user.create({
      data: u
    });

    console.log(`Created user with ID ${user.id}`);
  }

  console.log("Finished seeding.");
};

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

console.log("Bottom of script");

Console Output:

C:\Programming\prisma-bug-repro> prisma db seed --preview-feature
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Running seed from prisma\seed.ts ...

Your database has been seeded.
C:\Programming\prisma-bug-repro>

Environment & setup

  • OS: Windows 10 Version 20H2 (OS build 19042.985)
  • Database: MySQL
  • Node.js version: 16.0.0

Prisma Version

prisma               : 2.23.0
@prisma/client       : 2.23.0
Current platform     : windows
Query Engine         : query-engine adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine     : migration-engine-cli adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules\@prisma\engines\migration-engine-wi
ndows.exe)
Introspection Engine : introspection-core adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules\@prisma\engines\introspection-engine-
windows.exe)
Format Binary        : prisma-fmt adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : adf5e8cba3daf12d456d911d72b6e9418681b28b
Studio               : 0.393.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 31
  • Comments: 18 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I can confirm this regression. We will fix this.

Same issue too with 2.26

@janpio Confirmed. The only reported occurrences for RedwoodJS are specific to Windows.

@Cyntheon not sure. I pushed what I did here if it can help you.

@ljosberinn I have isolatedModules=true as well. Had to include this script in package.json to fix this issue:

package.json

{ 
  "scripts": {
    "ts-node": "ts-node --compiler-options {\\\"module\\\":\\\"commonjs\\\"}"
  }
}

@Cyntheon not sure. I pushed what I did here if it can help you.

@ljosberinn I have isolatedModules=true as well. Had to include this script in package.json to fix this issue:

package.json

{ 
  "scripts": {
    "ts-node": "ts-node --compiler-options {\\\"module\\\":\\\"commonjs\\\"}"
  }
}

This worked for me

Same issue too with 2.26

I added this script to my package.json, just as the documentation asks for:

"scripts": {
  "ts-node": "ts-node --compiler-options \"{\\\"module\\\":\\\"commonjs\\\"}\""
}

It´s important to have ts-node installed to execute the script.

And i added this option in my tsconfig.json:

{
  "compilerOptions": {
    "isolatedModules": true
  }
}

I execute npx prisma db seed --preview-feature, and my script in the seed.ts file worked.

Are you all on Windows where seeding is not working? I updated the title of this issue to reflect this assunption, so please speak up if that is not true (and optimally open a new issue with exactly your situation so we can track this separately!).

Yes it appears to be specific to Windows. My teammate is unable to seed our database (he’s on Windows) while I am able to (I’m on macOS).

In case someone is stuck with this issue, I resolved this with the following:

prisma/seed.ts:


const prisma = new PrismaClient();

export const seed = async () => {
  // prisma upserts

  process.exit(0);
};
seed();

Weird… I’ve tried multiple variations of that and none of them seem to be working either. Do you know what part of that code resolves the issue? Could it be something else you did?

@MarcosJBM glad that worked for you. We are reworking the seeding feature at the moment. The gist of it is that we want seeding to always work like that ts-node escape hatch, but with a clearer naming and guidance in the CLI. Would you be interested in testing a preview version of the feature and giving us your feedback? (that offer applies to anyone reading this message).

🗒️ We heard the feedback and we are planning a redesign of db seed (currently planned to be released early September)

How it will work is that a prisma.seed property in the package.json of your project will be required if you want to use prisma db seed.

This property is where you will be able to put any command you want to execute your seed script.

More info/details & how to try it out now in: https://github.com/prisma/prisma/issues/8732

Super happy to read that 😃 Stay tuned for moe, but we hope to deliver something stable and without any surprise when db seed comes out of preview. It’s a bit tricky to release because seeding is also used by prisma migrate dev, which is stable so we don’t want to break it.

@tomhoule Assuming the preview version of the feature is prisma@2.27.0-integration-db-seed-new-behavior.4, it appears to work quite nicely on Windows! I am working with a dedicated database package that contains the schema and migrations that can then be imported by projects accessing the shared database and both in the database package as well as one depending project the setup appears to work as expected (including seeding on prisma migrate reset) 👍

This was tested with a seed.js file with the script in the body; not as export. This setup didn’t seem to work with 2.26.0.

Adjusting my tsconfig leads to the same situation in this repo. With tsconfig.compilerOptions.isolatedModules active however, this error is thrown before even reaching my seed.ts: image

In case someone is stuck with this issue, I resolved this with the following:

prisma/seed.ts:


const prisma = new PrismaClient();

export const seed = async () => {
  // prisma upserts

  process.exit(0);
};
seed();