Prisma generated types not updating - postgresql

I use Prisma as an ORM and had to change remove some columns and add others. I've already updated the database with the new columns and recreated the prisma schema but the problem is that now every time I try to insert a new entry, I get this error:
Unknown arg "picture" in create.picture for type usersCreateInput. Did you mean "image"?
"picture" is one of the new columns and "image" is one of the deleted ones, it seems that the generated types are not being updated properly, cause one of the new columns is there (email_verified), but the other (picture) isn't.
type usersCreateInput {
name?: String | Null
email?: String | Null
email_verified?: DateTime | Null
image?: String | Null
mentor?: Boolean | Null
weekly_time?: Int | Null
journey?: String | Null
pronouns?: String | Null
occupation?: String | Null
description?: String | Null
created_at?: DateTime
updated_at?: DateTime
skills?: usersCreateskillsInput | List<String>
boards?: boardsCreateNestedOneWithoutUsersInput
}
These are exactly the steps I did:
Changed the init.sql file used by Docker
Recreated the DB based on the new init.sql
Deleted the existent schema, then npx prisma init
Run npx prisma db pull and npx prisma generate to recreate the schema based on the already existent database,and it's important to note that I checked the schema and it's correct:
model users {
id Int #id #default(autoincrement())
name String? #db.VarChar(255)
email String? #unique(map: "email") #db.VarChar(255)
email_verified Boolean?
picture String?
mentor Boolean?
skills String[]
weekly_time Int?
journey String? #db.VarChar(255)
pronouns String? #db.VarChar(255)
occupation String?
description String? #db.VarChar(255)
created_at DateTime #default(now()) #db.Timestamptz(6)
updated_at DateTime #default(now()) #db.Timestamptz(6)
boards boards?
sessions sessions[]
}
Is there a way to completely reset prisma? I've also tried to remove node_modules and install everything again but it didn't solve.

If you ever run into a problem, where you think TypeScript is not working properly, you can always try to restart TypeScipt language server by pressing Ctrl+Shift+P and typing >Restart TS server. Make sure to navigate to a .ts file first!
This way you don't have to re-open VSCode.

I had the same problem. The solution was to run the command npm i to delete the current Prisma Client (no need to delete node_modules), and then run npx prisma generate to re-create a new Prisma Client.
Hereafter I still had an error when trying to query the new column, but then the solution was closing and re-opening the editor, in my case VS Code.

Related

Why can't I query certain fields in Prisma?

I need to be able to access certain fields of a Prisma model, but they always come back undefined even though I can see that the data exists in Prisma Studio.
Here is my model for Goods in schema.prisma:
model Good {
id String #id #default(cuid())
title String
completed Boolean #default(false)
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
user User #relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)
userId String
}
Each Good has an id, title, etc. in addition to a user. However, when I log a Good to the console, it seems I can only access the title and id of the item. All the other expected fields are not present, and come back as undefined when I try to access them (i.e. console.log(good.user). Here's the output of console.log({good}):
{id: 'clcxxla0g0008ts5qjk248fzk', title: 'Title of good'}
What I need is to be able to query the user or userId for each Good, but I can't find a way to do this.
For more info, here is what what Prisma Studio data looks like:

What should I use as validation tool using NestJS and MongoDB?

Let's say, I am creating application using NestJS. I use MongoDB as a database and mongoose as ODM. So, NestJS has it's own way to validate data - ValidationPipe's. On the other side, there is mongoose built-in ways to validate data that being pushed into DB.
The question is - can I use only NestJS validation or do I need also second-check my data using mongoose validation tools?
Personally, I can't see why should I use additional layer of validation if I already have NestJS validation. But maybe there is best-practice or something?
Each database validates input data and emits an error if found. However, you must take into account your schema file. For instance, if you are using Prisma(It doesn't actually matter) and you have a User model like below
model User {
id String #id #default(auto()) #map("_id") #db.ObjectId
email String #unique
password String
name String
image String?
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
##map("user")
}
As you can see there is only one optional property "image". Whether you pass a value for image property or not, the database will insert the data as a row. On the other hand, the properties without "?" mark at the end of the type, will not be stored if you don't pass them and emit an error.
Therefore, if you modeled schema according to your business logic, you don't need to validate twice but only add an exception handling like the one below.
const user = await this.usersService.findOne('id...')
.catch((e) => new NotFoundException({ message: 'No user found.' }))

Use a field's generated value as another field's default value in prisma?

I have this User model in prisma (postgres), I want to use the same value cuid() generates for id in username. How do I achieve that?
model User {
id String #id #default(cuid())
username String? #unique
}
I know I can just regenerate cuid() for a different value, it would still work for what I am working on, but I want user's id and username to be same by default.

Postgres NestJS Prisma migration - Database error code: 23502 column of relation contains null values

I updated my Prisma ORM database model by adding two more fields 'ahash' and 'sAddress' to the 'invitation' table.
The table already contains 6 rows of data.
When I try to migrate the new changes to the Postgresql database, I get the error Database error code: 23502. ERROR: column "aHash" of relation "Invitation" contains null values.
How do I cater for the null values and migrate the model updates smoothly onto the database?
Give me a Step by step account please. I'm new to Prisma migration.
Thanks in advance!
The Prisma invitation model looks like below.
model Invitation {
id String #id #db.Uuid
workId String #db.Uuid
work Work #relation(fields: [workId], references: [id])
status RequestStatus
coId String #db.Uuid
oSignature String
note String
aHash String
sAddress String
createdAt DateTime
respondedAt DateTime
}
The problem here is that ahash and sAddress are both mandatory fields. However, they do not exist for the 6 existing rows/records in your database.
If you want to add new columns to an existing database without causing data loss you need to make sure the new columns/field are optional. This can be done in Prisma by marking the type of the field with a ?.
If you need the field to be mandatory, you could do it in three steps:
Create the new ahash and sAddress fields as optional first in your prisma schema and run a migration.
Run a script to update all existing records, so that they have a value for the ahash and sAddress fields.
Mark both fields as mandatory in your Prisma schema and run a migration.
In step 3, you will no longer get the error because none of the records contain a null value for the ahash and sAddress fields.

Prisma #db.Time(x) - what is x?

After running the npx prisma introspect in the console, the startTime attribute was set to #db.Time(6) in my schema.prisma file.
model Table {
id String #id #default(uuid())
startTime DateTime #map("start_time") #db.Time(6)
}
What does x mean in #db.Time(x) in Prisma schema? Documentation link
P.S. I use PosgreSQL as database
The x is the precision to store in the time type which can be anything from 0-6. You can read more this here.