Dbugger
Dbugger
PPrisma
Created by Dbugger on 6/10/2024 in #help-and-questions
Getting total count of records with one Query
Is it possible to get a _count of all records when doing a .findMany() query, without having to do a second .count() query? I know it works for relationships, but I dont know how it works for simple queries.
2 replies
PPrisma
Created by Dbugger on 6/7/2024 in #help-and-questions
I can't disconnect a many-to-many relationship
Disconnecting the previous contact relationships is giving me a lot of headache. What's wrong with this statement?
→ 16 update: ({ where, data }) => prismaModel.update({
where: {
id: "80d02590-489c-4a76-80c8-dafb86c94f3f",
organizationId: "803f8966-f808-4da6-96cf-354d355f0f85"
},
data: {
number: "0JD-05242",
dateIssued: new Date("2024-06-07T00:00:00.000Z"),
dueDate: new Date("2024-06-21T00:00:00.000Z"),
sender: "Berge, Lehner and Grimes\r\n350 Marilie Inlet\r\nLake Jovanichester, New Jersey 92127-2846\r\nBelize",
recipient: "Murphy LLC\r\n9176 Mante Fall\r\nLongview, Mississippi, 65850-0376\r\nRussian Federation",
items: [
{
taxRate: 0,
quantity: 1,
unitPrice: 0,
description: ""
}
],
status: "Draft",
footnotes: "",
purchaseOrder: "",
contacts: {
disconnect: {
contactId: {
notIn: [
"ab819c7a-3125-489a-8226-aa4374a2e7fd"
]
}
},
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
create: [
{
contact: {
connect: {
id: "ab819c7a-3125-489a-8226-aa4374a2e7fd"
}
}
}
]
},
attachments: {
create: [],
deleteMany: {
id: {
in: []
}
}
}
}
})

Argument `disconnect`: Invalid value provided. Expected InvoiceContactWhereUniqueInput[], provided Object.
→ 16 update: ({ where, data }) => prismaModel.update({
where: {
id: "80d02590-489c-4a76-80c8-dafb86c94f3f",
organizationId: "803f8966-f808-4da6-96cf-354d355f0f85"
},
data: {
number: "0JD-05242",
dateIssued: new Date("2024-06-07T00:00:00.000Z"),
dueDate: new Date("2024-06-21T00:00:00.000Z"),
sender: "Berge, Lehner and Grimes\r\n350 Marilie Inlet\r\nLake Jovanichester, New Jersey 92127-2846\r\nBelize",
recipient: "Murphy LLC\r\n9176 Mante Fall\r\nLongview, Mississippi, 65850-0376\r\nRussian Federation",
items: [
{
taxRate: 0,
quantity: 1,
unitPrice: 0,
description: ""
}
],
status: "Draft",
footnotes: "",
purchaseOrder: "",
contacts: {
disconnect: {
contactId: {
notIn: [
"ab819c7a-3125-489a-8226-aa4374a2e7fd"
]
}
},
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
create: [
{
contact: {
connect: {
id: "ab819c7a-3125-489a-8226-aa4374a2e7fd"
}
}
}
]
},
attachments: {
create: [],
deleteMany: {
id: {
in: []
}
}
}
}
})

Argument `disconnect`: Invalid value provided. Expected InvoiceContactWhereUniqueInput[], provided Object.
3 replies
PPrisma
Created by Dbugger on 5/11/2024 in #help-and-questions
Refactoring schema
I have the following schema:
model Invoice {
id Int @id @default(autoincrement())

// ...

// Relations
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
organizationId Int
attachments Attachment[]
}

model Attachment {
id Int @id @default(autoincrement())
name String
path String

// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

// Relations
invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
invoiceId Int
}

model Organization {
id Int @id @default(autoincrement())

// ...

// Relationships
invoices Invoice[]

logo Logo? @relation(fields: [logoId], references: [id])
logoId Int? @unique
}

model Logo {
id Int @id @default(autoincrement())
name String
path String

// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Organization Organization?
}
model Invoice {
id Int @id @default(autoincrement())

// ...

// Relations
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
organizationId Int
attachments Attachment[]
}

model Attachment {
id Int @id @default(autoincrement())
name String
path String

// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

// Relations
invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
invoiceId Int
}

model Organization {
id Int @id @default(autoincrement())

// ...

// Relationships
invoices Invoice[]

logo Logo? @relation(fields: [logoId], references: [id])
logoId Int? @unique
}

model Logo {
id Int @id @default(autoincrement())
name String
path String

// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Organization Organization?
}
Both the logos and Attachment are the same thing: files. So I thought that maybe I should refactor it, into the same table, but then apparently I am forced to do this
model File {
id Int @id @default(autoincrement())
name String
path String

// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

// Relations
organization Organization? @relation(fields: [organizationId], references: [id])
organizationId Int?
invoice Invoice? @relation(fields: [invoiceId], references: [id])
invoiceId Int?
}
model File {
id Int @id @default(autoincrement())
name String
path String

// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

// Relations
organization Organization? @relation(fields: [organizationId], references: [id])
organizationId Int?
invoice Invoice? @relation(fields: [invoiceId], references: [id])
invoiceId Int?
}
Which seems like a waste, because now the model File will need to update, everytime there is a new model that references the file. It does not scale well. What would be the best way to optimize my Schema?
4 replies