DateTime without time / only data? It is possible in Prisma? - postgresql

I can in Prisma create columns without data, without time?
my model at the moment:
model modelName {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
lastNumber Int
}
Actual results:
2 2022-04-19 12:28:04.591+00 45
I want to generate records like this:
2 2022-04-19 45

To store only the date portion you could use the native #db.date attribute.
That would just store the date and not store the time.
In your schema file you could update the model as below to just store the date portion:
model modelName {
id Int #id #default(autoincrement())
createdAt DateTime #default(now()) #db.Date
lastNumber Int
}
Here's a reference to Prisma's PostgreSQL DateTime attribute: Reference

Related

How to have several models in one - Prisma

Currenty I am going to try define multiple models with the same name but I have a warning when I define Two models with same name:
For example: This is my file "parameters.prisma"
// ◮◮◮ GENERATED BY AURORA ◮◮◮
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model Reminder {
id String #id #default(auto()) #map("_id") #db.ObjectId
title String
description String?
codeClass String?
userId String?
createdAt DateTime #default(now())
updatedAt DateTime? #updatedAt
##map("Parameters")
}
model ActivityType {
id String #id #default(auto()) #map("_id") #db.ObjectId
title String
description String?
codeClass String?
userId String?
createdAt DateTime #default(now())
updatedAt DateTime? #updatedAt
##map("Parameters")
}
The part: '##map("Parameters")' changes the name of models Reminder and ActivityType. I need to put those models as a model named Parameters. In my MongoDB, I need to have a only model named Parameters that have data of Reminder and ActivityType models. Its fields of both models will change in a future. Its only an example in this time.
Well. How I can do this part?. Prisma show me this error: Or exists other best way for do this part?

Multiple relation to the same field prisma postgresql

I want to create this database deisng https://i.stack.imgur.com/2QYip.png
I need to make model of Station and TrackSegment (A track segment ties two stations together.)
This is my prisma schema but it give me this error "Error validating model 'station': the unique index definitions refers to the field code multiple times.
model Station {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
name String
code String #unique
}
model TrackSegment {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
sourceId String
destinationId String
stationCode Station #relation(fields: [destinationId, sourceId], references: [code, code])
}
I imagine this is how the data will look.
Station: [
{
name: 'kalisetail'
code: 'KLS
},
{
name: 'rogojampi'
code: 'RGJ
}]
then the TrackSegment is
id: 1
sourceId: KLS
destinationId: RGJ
both sourceId and destinationId refer to the same field(code) of model Station
You will need to add a second Station relation on your TrackSegment field to differentiate between the source the destination.
model Station {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
name String
code String #unique
sourceSegments TrackSegment[] #relation("source")
destinationSegments TrackSegment[] #relation("destination")
}
model TrackSegment {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
sourceId String
destinationId String
sourceStationCode Station #relation(name: "source", fields: [sourceId], references: [code])
destinationStationCode Station #relation(name: "destination", fields: [destinationId], references: [code])
}
Notice I have always disambiguated the relations by giving them different names.

Define schema with default date 12 months in the future

How would I define an endDate to be 12 months in the future in my schema.prisma. For example:
model Subscription {
id Int #id #default(autoincrement())
startDate DateTime #default(now())
endDate DateTime #default( How to define 1 year in the future?)
...
}
You would need to create a custom function that returns the time after one year and call it via the dbgenerated function.
Example: The function created in the database is called get_end_time. You can define it in schema as
model Subscription {
id Int #id #default(autoincrement())
startDate DateTime #default(now())
endDate DateTime #default(dbgenerated("get_end_time()"))
}

One-to-many and may-to-many relation between two prisma model

I am working on a side project and I came to an issue that I'm not sure how to solve. I have created two models, one for User and one for Project. The relation between them is many-to-many, as many users can have many projects, but i would also like to add a createdBy to the Project model, and this should be one-to-one as each project can only have one user who creates it. This is how my models are looking:
model User {
id String #id #default(cuid())
name String?
email String? #unique
emailVerified DateTime?
image String?
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
accounts Account[]
sessions Session[]
projects Project[]
Project Project[] #relation("userId")
}
model Project {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
name String
favourite Boolean #default(false)
archived Boolean #default(false)
users User[]
about String?
archivedAt String?
deletedAt String?
createdBy User #relation("userId", fields: [userId], references: [id])
userId String
}
The error that is getting from prisma migrate dev is
Step 1 Added the required column userId to the Project table without a default value. There are 2 rows in this table, it is not possible to execute this step.
Not sure what I am doing wrong as am pretty much a db modeling novice. Any help would be appreciated.
To achieve Many-to-Many with One-To-Many you need to set your schema.prisma file like:
model User {
id String #id #default(cuid())
name String?
email String? #unique
emailVerified DateTime?
image String?
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
projects Project[]
userProjects UserProjects[]
}
model Project {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
name String
favourite Boolean #default(false)
archived Boolean #default(false)
about String?
archivedAt String?
deletedAt String?
createdBy User #relation(fields: [user_id], references: [id])
user_id String
projectUsers UserProjects[]
}
model UserProjects {
project Project #relation(fields: [project_id], references: [id])
project_id Int
user User #relation(fields: [user_id], references: [id])
user_id String
##id([project_id, user_id])
}
It should migrate successfully.

Entity money transactions schema design

I'm trying to create a many-to-many relationship between two models: Entity and Taransaction
model Entity {
id String #id #default(cuid())
name String
purchases Transactions[] // references the source field on Transaction
sales Transaction[] // references the destination field on Transaction
}
model Transaction {
source Entity
destination Entity
amount Float
date DateTime #default(now())
}
What I want is to be able to retrieve all the Entity's purchases which points to the source of the transaction and all the Entity's sales which points to the destination of the transaction.
My question is, what would the schema for this relation would look like using Prisma 2?
Could you try this:
model Entity {
id String #id #default(cuid())
name String
purchases Transact[] #relation("purchases")
sales Transact[] #relation("sales")
}
model Transact {
id String #id #default(cuid())
source Entity #relation("purchases", fields: [sourceId], references: [id])
destination Entity #relation("sales", fields: [destinationId], references: [id])
amount Float
date DateTime #default(now())
sourceId String
destinationId String
}