prisma db pull doesn't see a new table - Stack Overflow
when i do command 'npx prisma db pull', Can i pull specific table?
database - Why "npx prisma db pull" is blocked on Introspecting? - Stack Overflow
Ecto equivalent for "prisma db pull"?
My backend is a fastify/Prisma to a PostgreSQL DB, I am much more comfortable with building my DB tables and constraints (PK, FK, etc) via SQL rather than Prisma's relationships schema.
My question is: If I continue to build my DB architecture directly in the db, is the Prisma db pull a reliable method to keep my Prisma schema up to date and accurate so I can get the typing's?
I just am able to comprehend and explain the relationship much better via SQL rather than via the model references.
I had a similar issue once and a quick check confirmed for me that it was the lack of security permissions granted for prisma on new table in the database itself.
Try this:
- Note the name of the database user that Prisma connects to the database with. You'll likely find this via your schema.prisma file, or perhaps via a
DATABASE_URLconfig setting in the related.envfile if you're using that with prisma. - Go into the database itself and ensure that database user which Prisma connects with has been granted sufficient security privileges to that new table. (note: what 'sufficient' is I cannot say since it depends on your own needs. At a guess, I'd say at least 'select' permission would be needed.)
- Once you've ensure that user has sufficient privileges, try running a
prisma db pullcommand once again.
For reference, another thing you could do is:
- cross-check against one of the other tables that is already in your database that works correctly with prisma.
- compare the security privileges of that old table with the security privileges of the new table and see if there are any differences.
If you use supabase and running this command and it returns something like this 'The following models were commented out because we couldn't retrieve columns for them. Please check your privileges.' or something similar regarding privileges, the solution is to go to your SQL editor from supabase and put this command and execute it
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgres;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO postgres;
Some prisma CLI commands(ex: npx prisma db pull) require DIRECT_URL to be passed as part of datasource object like below.
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}
You can read about it more here: Prisma Datasource fields
The Direct Url uses Session Mode URL and connects via port:5432. Whereas Database URL uses Transaction Mode URL and connect via port:6543
Note: I'm using Supabase, which is a PostgreSQL database.
I just fired up a new project to verify that this works:
npm init
npm install prisma --save-dev
Add .env file with database information, e.g.
DATABASE_URL="postgresql://postgres.xxxxyyyyzzzz:passpasspass@aws-0-us-west-1.pooler.supabase.com:5432/postgres?schema=public"
where postgres.xxxxyyyyzzzz is they user name, and passpasspass is the password
Then run
npx prisma init --datasource-provider postgresql
And finally:
npx prisma db pull
Which produces the prisma/schema.prisma file, first few lines:
/// This model contains row level security and requires additional setup for migrations. Visit https://pris.ly/d/row-level-security for more info.
model locations {
location_name String @db.VarChar(255)
street_address String @db.VarChar(255)
city String @db.VarChar(100)
state_province String @db.VarChar(100)
postal_code String @db.VarChar(20)
unit_number String? @db.VarChar(20)
notes String?
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
service_requests service_requests[]
}
Edit: Then I added a new table Foo, and ran npx prisma db pull again, adds the table to the schema
/// This model or at least one of its fields has comments in the database, and requires an additional setup for migrations: Read more: https://pris.ly/d/database-comments
/// This model contains row level security and requires additional setup for migrations. Visit https://pris.ly/d/row-level-security for more info.
model foo {
id BigInt @id @default(autoincrement())
created_at DateTime @default(now()) @db.Timestamptz(6)
}