Prisma seeding and migrations
astral-lab
HOBBYOP

14 days ago

Hey, I have a React Router 7 project that uses Prisma as its ORM. However, I am completely lost at how to seed the database/run migrations. I keep getting Error: Could not find Prisma Schema that is required for this command.

Solved$10 Bounty

66 Replies

Does your code work locally?


astral-lab
HOBBYOP

14 days ago

Yes, everything works as intended.


astral-lab
HOBBYOP

14 days ago

I've been at this for 12 hours and really have no idea what is wrong.


Did you commit your schema files?


astral-lab
HOBBYOP

14 days ago

Yes


astral-lab
HOBBYOP

14 days ago

Folder structure is:

/app
/prisma
/public


astral-lab
HOBBYOP

14 days ago

Build command: npm run build
Start command: npm run start

 "scripts": {
    "build": "react-router build",
    "start": "prisma migrate deploy --schema=./prisma/schema.prisma && prisma generate --schema=./prisma/schema.prisma && react-router-serve ./build/server/index.js",
    "dev": "react-router dev",
    "typecheck": "react-router typegen && tsc",
    "format": "prettier --write ."
  },
  "prisma": {
    "schema": "./prisma/schema.prisma",
    "seed": "ts-node ./prisma/seed.ts"
  },

astral-lab
HOBBYOP

14 days ago

I've tried a lot of different permutations of the scripts and really am lost 😅


diogoribeirodev
FREETop 5% Contributor

14 days ago

Hey, you should move prisma generate to the build step instead since you only need to run it when code changes.

Update your scripts:

```json
"scripts": {
"build": "prisma generate --schema=./prisma/schema.prisma && react-router build",
"start": "prisma migrate deploy --schema=./prisma/schema.prisma && prisma db seed && react-router-serve ./build/server/index.js",
"dev": "react-router dev",
"typecheck": "react-router typegen && tsc",
"format": "prettier --write ."
}


diogoribeirodev
FREETop 5% Contributor

14 days ago

Hey, you should move prisma generate to the build step instead since you only need to run it when code changes.

Update your scripts:

"scripts": {
  "build": "prisma generate --schema=./prisma/schema.prisma && react-router build",
  "start": "prisma migrate deploy --schema=./prisma/schema.prisma && react-router-serve ./build/server/index.js",
  "dev": "react-router dev",
  "typecheck": "react-router typegen && tsc",
  "format": "prettier --write ."
}

Private networking is not available during build.


OP won’t be able to migrate or seed.


astral-lab
HOBBYOP

14 days ago

Should I put migrate/seed in the pre deploy step instead?


Yes, but I doubt that’ll fix the primary issue as it’s not about not being able to find your database, but rather the Prisma schema itself.
But you can give it a try.


astral-lab
HOBBYOP

14 days ago

I managed to get a different error, I'll post it in a second, but the schema has now been found which is good.


Oh thats good.


astral-lab
HOBBYOP

14 days ago

(No subject)
meta: {
PrismaClientKnownRequestError:
Invalid `prisma.map.create()` invocation:
modelName: 'Map',
driverAdapterError: DriverAdapterError: TableDoesNotExist
at PrismaPgAdapter.onError (file:///app/node_modules/@prisma/adapter-pg/dist/index.mjs:651:11)
The table `public.Map` does not exist in the current database.
at PrismaPgAdapter.performIO (file:///app/node_modules/@prisma/adapter-pg/dist/index.mjs:646:12)
at zr.handleRequestError (/app/node_modules/@prisma/client/src/runtime/RequestHandler.ts:237:13)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at zr.handleAndLogRequestError (/app/node_modules/@prisma/client/src/runtime/RequestHandler.ts:183:12)
at async PrismaPgAdapter.queryRaw (file:///app/node_modules/@prisma/adapter-pg/dist/index.mjs:566:30)
at zr.request (/app/node_modules/@prisma/client/src/runtime/RequestHandler.ts:152:12)
at async e.interpretNode (/app/node_modules/@prisma/client-engine-runtime/src/interpreter/query-interpreter.ts:182:26)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async a (/app/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:808:24)
at async main (/app/prisma/seed.ts:2022:5) {
code: 'P2021',
at async e.interpretNode (/app/node_modules/@prisma/client-engine-runtime/src/interpreter/query-interpreter.ts:209:41)
at async e.interpretNode (/app/node_modules/@prisma/client-engine-runtime/src/interpreter/query-interpreter.ts:268:41)
at async e.run (/app/node_modules/@prisma/client-engine-runtime/src/interpreter/query-interpreter.ts:93:23)
at async e.execute (/app/node_modules/@prisma/client/src/runtime/core/engines/client/LocalExecutor.ts:81:12)
at async qt.request (/app/node_modules/@prisma/client/src/runtime/core/engines/client/ClientEngine.ts:492:22) {
cause: [Object]
}
},
clientVersion: '7.4.1'
}
An error occurred while running the seed command:
Error: Command failed with exit code 1: tsx prisma/seed.ts

astral-lab
HOBBYOP

14 days ago

I have confirmed that the env is being read by seed.ts


Did you migrate it before seeding?


Seems like it’s trying to modify a table that doesn’t exist yet.


astral-lab
HOBBYOP

14 days ago

My Pre-deploy command is npx prisma migrate deploy && npx prisma db seed


astral-lab
HOBBYOP

14 days ago

I assumed migrate runs first in this syntax


astral-lab
HOBBYOP

14 days ago

Here is the updated scripts section too (apologies for the code spam 😅)

  "scripts": {
    "build": "DATABASE_URL=\"postgres://placeholder:5432\" npx prisma generate && react-router build",
    "start": "react-router-serve ./build/server/index.js",
    "dev": "react-router dev",
    "typecheck": "react-router typegen && tsc",
    "format": "prettier --write ."
  },
  "prisma": {
    "seed": "tsx ./prisma/seed.ts"
  },

Go to your database service in Railway and go to the database tab and check if the desired tables exist.


astral-lab
HOBBYOP

14 days ago

No tables exist other than a prismamigration


This btw. You’ll need to move migration and seed command to the pre-deploy step.


astral-lab
HOBBYOP

14 days ago

I believe I've done this, it's under the Deploy header right?



astral-lab
HOBBYOP

14 days ago

Cool, yep, that's done


astral-lab
HOBBYOP

14 days ago

I'm sure this is close to being solved


In your database URL variable in the shell command, try using the provided reference variable.
Add a variable entry called DATABASE_URL in your service with the value ${{Postgres.DATABASE_URL}}, and remove the existing DATABASE_URL=… in your script.


astral-lab
HOBBYOP

14 days ago

So just replace the fake db env with the ${{}} syntax?


The {{}} goes in your Railway dashboard, the variables tab in your service.


Wait hold up.


astral-lab
HOBBYOP

14 days ago

I already have one, unless I misunderstand. My service has the private URL


Try deleting that table. Perhaps Prisma thinks it already successfully migrated everything when it hasn’t yet.
Then redeploy the service.


astral-lab
HOBBYOP

14 days ago

Didn't see a way to delete the migration table, but I've redeployed and will see


astral-lab
HOBBYOP

14 days ago

I might just delete the database, maybe that'll do it



Okay try this


In your local environment, set your database URL to be your database’s public URL. (Note that this may incur some egress costs)


Then run npx prisma migrate status (or pnpx, depending on your pkg manager).


See if Prisma thinks everything has been applied.


astral-lab
HOBBYOP

14 days ago

It says "Database schema is up to date"


astral-lab
HOBBYOP

14 days ago

I also get the same error as before when running the seed locally with the public url


Try prisma migrate reset.


astral-lab
HOBBYOP

14 days ago

Just did that and applied the db seed again, same error 😅


Sanity check, you’re operating on the railway database right?


astral-lab
HOBBYOP

14 days ago

Yes, 100%


Try prisma db push --force-reset.


And check if any tables exist in your Railway dashboard.


astral-lab
HOBBYOP

14 days ago

I just ran the following:

npx prisma migrate dev
npx prisma generate
npx prisma db seed

Now all the tables have appeared! However, I'd rather have this run automatically when deploying.


astral-lab
HOBBYOP

14 days ago

Should I just move generate into the pre deploy then, as it works in this order?


Interesting… doing dev migrate did it.


Did you previously have anything in prisma/migrations/ directory prior to this working?


astral-lab
HOBBYOP

14 days ago

In the branch I pushed, I don't think so


I think what you needed was to generate migration files. This was why Prisma couldn’t find the tables. Your deployment had nothing to migrate from.


You should be running migrate dev before pushing to remote.


This will create the migration files, giving your Railway deployment something to migrate from.


Think that was the issue here.


After you ran migrate dev locally, you should be seeing some newly generated migration files in the prisma directory.


But good to know it’s fixed.
Also, use migrate deploy in your scripts. Don’t use dev in a prod environment.


But yes, keep that in mind whenever you make any changes to the schema from now on.


astral-lab
HOBBYOP

14 days ago

Perfect, so whenever I make changes locally I run migrate dev and push that? Then Railway will know what to do with them. Sounds good. Really appreciate the help and your time 😊


astral-lab
HOBBYOP

14 days ago

I'm not sure how the Bounty Board works, but don't you get a reward or something for helping?


Think Railway needs to mark the solution for me to earn it.


Status changed to Solved noahd 14 days ago


Loading...