How to have several models in one - Prisma - 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?

Related

Prisma update query only allowing an update by the ID field and not anything else

const response = await prisma.teamMember.update({
where: {
teamId,
userId: memberToEditId,
},
data: {
role,
},
});
Argument where of type TeamMemberWhereUniqueInput needs exactly one argument, but you provided teamId and userId. Please choose one. Available args:
type TeamMemberWhereUniqueInput {
id?: String
}
Unknown arg `teamId` in where.teamId for type TeamMemberWhereUniqueInput. Did you mean `id`? Available args:
type TeamMemberWhereUniqueInput {
id?: String
}
Unknown arg `userId` in where.userId for type TeamMemberWhereUniqueInput. Did you mean `id`? Available args:
type TeamMemberWhereUniqueInput {
id?: String
}
Hey guys. I'm trying to update a specific document based off of a specific value(s) in my table, but it only seems to let me use the primary key for the table? My schema looks like:
model TeamMember {
id String #id #default(cuid())
teamId String
userId String
role Role #default(MEMBER)
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
team Team #relation(fields: [teamId], references: [id], onDelete: Cascade)
user User #relation(fields: [userId], references: [id], onDelete: Cascade)
}
model Team {
id String #id #default(cuid())
name String
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
TeamMember TeamMember[]
}
model User {
id String #id #default(cuid())
name String?
email String? #unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
TeamMember TeamMember[]
theme Theme #default(LIGHT)
}
To fix this, temporarliy I can make a separate findFirst query, and use the returned row to get the ID of the row which i want to update. This is fine, however I know it can be done without doing this, and it is a little ugly having two queries when one can work just fine.
Any help would be greatly appreciated.
Prisma needs to uniquely identify a single record which needs to be updated, due to which the whereUnique constraint is enforced.
For now, you can use updateMany as a workaround for this, this unique constraint is not enforced for updateMany

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.

How to make id autoincrement in schema.prisma?

I am writing a postgreSQL database. The ID must be auto-incrementing.
model User {
id String #id #default(cuid())
name String?
email String? #unique
emailVerified DateTime? #map("email_verified")
image String?
createdAt DateTime #default(now()) #map(name: "created_at")
updatedAt DateTime #updatedAt #map(name: "updated_at")
posts Post[]
accounts Account[]
sessions Session[]
##map(name: "users")
}
Result
If you write the ID manually, you can enter any value as in the first entry, but autocomplete generates the code as in the second entry. I got the code from the site Vercel.
You need to use autoincrement function.
Here's how it would look in User model:
model User {
id Int #id #default(autoincrement())
name String?
email String? #unique
emailVerified DateTime? #map("email_verified")
image String?
createdAt DateTime #default(now()) #map(name: "created_at")
updatedAt DateTime #updatedAt #map(name: "updated_at")
posts Post[]
accounts Account[]
sessions Session[]
##map(name: "users")
}

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.

prisma2 how to relate one-to-many and many-to-one?

I need some help to understqnd how to set my data modelisation.
What i'm trying to do is :
Reference a group ID in User model. A user can only be in one group, this is what i've done below.
Reference in Group Model all users that are inside a group. This is what I need help with.
model User {
id String #id #default(uuid())
email String #unique
role Role #default(USER)
group Group? #relation(fields: [group_id], references: [id], onDelete: SetNull)
group_id String
created_at DateTime #default(now())
updated_at DateTime #updatedAt
##map("user")
}
model Group {
id String #id #default(uuid())
name String
users User[]
created_at DateTime #default(now())
updated_at DateTime #updatedAt
deleted Boolean #default(false)
##map("group")
}
How can I make this list of User when I already have users User[] for the relation in User model ?
(BTW i've read the doc but i'm totally lost...)
I think your code is correct, you can use prisma-client to test
This is an example of this relation
model Agents {
id Int #id #default(autoincrement()) #db.UnsignedInt
name String? #db.VarChar(255)
owner String? #db.VarChar(255)
address String? #db.Text
lat String? #db.VarChar(10)
lng String? #db.VarChar(10)
tel String? #db.VarChar(13)
mobile String? #db.VarChar(11)
workTime String? #db.VarChar(255)
province Provinces #relation(fields: [provinceId], references: [id])
provinceId Int #db.UnsignedInt
createDateTime DateTime #default(now()) #db.Timestamp(0)
updateDateTime DateTime? #db.Timestamp(0)
##index([provinceId], name: "provinceId")
##map(name: "agents")
}
model Provinces {
id Int #id #default(autoincrement()) #db.UnsignedInt
name String? #db.VarChar(255)
coords String? #db.Text
createDateTime DateTime #default(now()) #db.Timestamp(0)
updateDateTime DateTime? #db.Timestamp(0)
agents Agents[]
##map(name: "provinces")
}