Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeding not working in 2.23.0 in Windows #7176

Closed
Cyntheon opened this issue May 20, 2021 · 18 comments
Closed

Seeding not working in 2.23.0 in Windows #7176

Cyntheon opened this issue May 20, 2021 · 18 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/regression A reported bug in functionality that used to work before. team/schema Issue for team Schema. topic: prisma db seed CLI: prisma db seed topic: windows

Comments

@Cyntheon
Copy link

Cyntheon commented May 20, 2021

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
@Cyntheon Cyntheon added the kind/bug A reported bug. label May 20, 2021
@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: seeding kind/regression A reported bug in functionality that used to work before. team/schema Issue for team Schema. and removed kind/bug A reported bug. labels May 20, 2021
@jpb06
Copy link

jpb06 commented May 22, 2021

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();

@Cyntheon
Copy link
Author

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?

@ljosberinn
Copy link

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

@jpb06
Copy link

jpb06 commented May 23, 2021

@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\\\"}"
  }
}

@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels May 23, 2021
@pantharshit00
Copy link
Contributor

I can confirm this regression. We will fix this.

@remioo
Copy link

remioo commented Jun 2, 2021

@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

@janpio janpio changed the title Seeding not working in 2.23.0 Seeding not working in 2.23.0 in Windows Jun 14, 2021
@janpio
Copy link
Member

janpio commented Jun 14, 2021

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!).

@davidchalifoux
Copy link

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

@thedavidprice
Copy link

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

@MikaStark
Copy link

Same issue too with 2.26

@janpio janpio modified the milestones: 2.26.0, 2.27.0 Jun 30, 2021
@MarcosJBM
Copy link

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.

@tomhoule
Copy link
Contributor

tomhoule commented Jul 9, 2021

@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).

@geisterfurz007
Copy link

geisterfurz007 commented Jul 10, 2021

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

@tomhoule
Copy link
Contributor

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.

@Jolg42
Copy link
Member

Jolg42 commented Aug 13, 2021

🗒️ 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:
#8732

@Jolg42 Jolg42 added this to the 2.31.0 / 3.0.x milestone Sep 3, 2021
@janpio
Copy link
Member

janpio commented Nov 19, 2021

This is now released - let us know if this works for you. Thanks!

@janpio janpio closed this as completed Nov 19, 2021
@kosmikgirl
Copy link

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();

After hours of research, only this worked for me. I've tried the other options to change package.json with no success.
Also, I am using masOS Monterey v12.

Thanks!

@benistary
Copy link

None of those worked for me. This worked:

"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/regression A reported bug in functionality that used to work before. team/schema Issue for team Schema. topic: prisma db seed CLI: prisma db seed topic: windows
Projects
None yet
Development

No branches or pull requests