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
- Generate a new nodejs project with
yarn init - Run
yarn add @prisma/clientandyarn add --dev @types/node prisma ts-node typescript - Run
prisma init - Set
DATABASE_URLin.envfile - Create
tsconfig.json - Run
prisma migrate dev --name init - Create
seed.tsin folder withschema.prisma - Run
prisma db seed --preview-feature - 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
- fix(seed): add empty export {} for TypeScript --isolatedModules Related https://github.com/prisma/prisma/issues/7176 — committed to prisma/prisma by Jolg42 3 years ago
- fix(seed): add empty export {} for TypeScript --isolatedModules (#7666) Related https://github.com/prisma/prisma/issues/7176 — committed to prisma/prisma by Jolg42 3 years ago
- fix(seed): add empty export {} for TypeScript --isolatedModules (#7666) Related https://github.com/prisma/prisma/issues/7176 — committed to Andrew-Colman/prisma by Jolg42 3 years ago
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=trueas well. Had to include this script inpackage.jsonto fix this issue:package.json
This worked for me
I added this script to my
package.json, just as the documentation asks for:It´s important to have
ts-nodeinstalled to execute the script.And i added this option in my
tsconfig.json:I execute
npx prisma db seed --preview-feature, and my script in the seed.ts file worked.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).
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-nodeescape 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.seedproperty in the package.json of your project will be required if you want to useprisma 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 onprisma migrate reset) 👍This was tested with a
seed.jsfile 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.isolatedModulesactive however, this error is thrown before even reaching my seed.ts:In case someone is stuck with this issue, I resolved this with the following:
prisma/seed.ts: