moosthuizen
moosthuizen
PPrisma
Created by thai on 7/21/2024 in #help-and-questions
Can't see relation field in Types
@thai Where is ProjectPage defined (being imported from)? If you are importing it from one of the types generated by Prisma Client, make sure to generate the client every time after making schema changes. If you are manually defining a ProjectPage type/interface, you will need to update this every time your schema.
4 replies
PPrisma
Created by Fervore on 7/22/2024 in #help-and-questions
Nested relation query help
Alternatively, my recommendation is to use the first query you listed above, and do the filtering you want in code.
6 replies
PPrisma
Created by Fervore on 7/22/2024 in #help-and-questions
Nested relation query help
@Fervore Seeing as there is a relation between Member and Payment, would the following work?
const result = prisma.member.findMany({
orderBy: {
id: 'desc',
},
include: {
payments: {
distinct:[membershipId], // Only get one payment for each membership
take: 1,
orderBy: { endDate: 'asc' },
include: {
membership: true
}
}
}
})
const result = prisma.member.findMany({
orderBy: {
id: 'desc',
},
include: {
payments: {
distinct:[membershipId], // Only get one payment for each membership
take: 1,
orderBy: { endDate: 'asc' },
include: {
membership: true
}
}
}
})
You can then flatten result[].payments[].membership into result[].new_property_with_membership_and_payment_fields or similar
6 replies
PPrisma
Created by Fervore on 7/22/2024 in #help-and-questions
Nested relation query help
@Fervore Would you post your schema.prisma?
6 replies
PPrisma
Created by JudgeJLo on 7/22/2024 in #help-and-questions
unrecognized keys some
paste*
4 replies
PPrisma
Created by JudgeJLo on 7/22/2024 in #help-and-questions
unrecognized keys some
@JudgeJLo Can you past your schema.prisma?
4 replies
PPrisma
Created by Nick on 3/25/2024 in #help-and-questions
Frequent constraint failed on id when using @id @default(autoincrement())
We find that if you delete a row and then insert that same row with the same id, we hit a prisma:error Unique constraint failed on the fields: (id).
PostgreSQL keeps a counter of what the next ID should be, and that counter doesn't change regardless of what data gets deleted. From PostgreSQL docs: https://www.postgresql.org/docs/current/sql-createsequence.html ... nextval and setval calls are never rolled back... nextval is the PG function that determines what the ID of a new inserted row should be.
Additionally, we find that if you use a transaction to insert multiple rows at once, we also hit prisma:error Unique constraint failed on the fields: (id).
I'm not sure how/which IDs are provided, but this probably fails for the same reason, being a mismatch between the IDs provided in .createMany and the IDs the DB would have assigned. @Nick It looks like you need to decide whether you want the DB to have full control over what the IDs should be, or whether you want your code to have that control.
5 replies
PPrisma
Created by Bean on 7/12/2024 in #help-and-questions
create many skip existing
You are literally describing a multi-colum unique contraint 🙂
13 replies
PPrisma
Created by Bean on 7/12/2024 in #help-and-questions
create many skip existing
@@unique([col1, col2])
13 replies
PPrisma
Created by Bean on 7/12/2024 in #help-and-questions
create many skip existing
You can do a unique constraint with multiple columns, which will make sure that the combination of data across all those columns specified in the contraint is unique, not the columns taken individually.
13 replies
PPrisma
Created by Bean on 7/12/2024 in #help-and-questions
create many skip existing
@Bean On PostgreSQL at least, skipDuplicates: true causes ON CONFLICT DO NOTHING to be included in the query (https://www.postgresql.org/docs/9.5/sql-insert.html#SQL-ON-CONFLICT). You can cause a conflict by defining a unique constraint on col1 and col2. This will cause new "duplicate" data to violate that constraint, which will then be skipped because of the DO NOTHING. Hopefully this gives you the behaviour that you are looking for?
13 replies
PPrisma
Created by ncls. on 7/12/2024 in #help-and-questions
How to reference/store a joined primary key?
no prob, would be curious to hear if that works. Let me know
10 replies
PPrisma
Created by ncls. on 7/12/2024 in #help-and-questions
How to reference/store a joined primary key?
Unless there's a strong reason not to, you can also consider generating a new primary key, while still enforcing a unique constraint with @@unique. Best of both worlds.
10 replies
PPrisma
Created by ncls. on 7/12/2024 in #help-and-questions
How to reference/store a joined primary key?
@ncls. See this page of the documentation: https://www.prisma.io/docs/orm/prisma-client/special-fields-and-types/working-with-composite-ids-and-constraints You can give a name to a composite key (what you referred to as the joined key). I'm not sure if you can then use that name to create the relation, but it's worth a try:
model FirstEntity {
id Int
category String
relations Relation[]

@@id("composite_key_1", [category, id])
}

model Relation {
id Int @id @default(autoincrement())
firstEntityId Int
firstEntityCategory String
relation FirstEntity @relation(fields: [firstEntityId, firstEntityCategory], references: [composite_key_1])
}
model FirstEntity {
id Int
category String
relations Relation[]

@@id("composite_key_1", [category, id])
}

model Relation {
id Int @id @default(autoincrement())
firstEntityId Int
firstEntityCategory String
relation FirstEntity @relation(fields: [firstEntityId, firstEntityCategory], references: [composite_key_1])
}
10 replies
PPrisma
Created by Guillaume630 on 7/18/2024 in #help-and-questions
Sort by nulls
@Guillaume630 If I understand what you mean correctly, maybe you are looking for extra conditions to add to the where clause? eg.
where: {
AND: [
{ TITLE: { not: null }},
{ TITLE: { not: "" }},
{ ID_CONCURRENT: { not: null }},
{ ID_CONCURRENT: { not: "" }},
// etc...
...where // Spread the existing where conditions
]
}
where: {
AND: [
{ TITLE: { not: null }},
{ TITLE: { not: "" }},
{ ID_CONCURRENT: { not: null }},
{ ID_CONCURRENT: { not: "" }},
// etc...
...where // Spread the existing where conditions
]
}
2 replies
PPrisma
Created by elie on 7/2/2024 in #help-and-questions
Error: Cannot fetch data from service:getaddrinfo ENOTFOUND aws-us-east-1.prisma-data.com
@elie Are you sure this is a Prisma thing? Regardless, usually I see ENOTFOUND when there is a DNS problem or I forget the protocol in a URL
4 replies
PPrisma
Created by Rarity on 6/28/2024 in #help-and-questions
nested transactions
haha, sounds elegant in its own way
6 replies
PPrisma
Created by Rarity on 6/28/2024 in #help-and-questions
nested transactions
@Rarity
6 replies
PPrisma
Created by Rarity on 6/28/2024 in #help-and-questions
nested transactions
Prisma.transaction needs to be provided with an array of Prisma promises. Depending on how much extra stuff is being done inside test1() and test2(), can you use the Prisma calls inside those functions directly? eg.
await prisma.transaction([
// prisma stuff to do #1
// prisma stuff to do #2
])
await prisma.transaction([
// prisma stuff to do #1
// prisma stuff to do #2
])
6 replies
PPrisma
Created by Tekipeps on 6/30/2024 in #help-and-questions
Relations issue
3 replies