KinglyEndeavors
KinglyEndeavors
PPrisma
Created by KinglyEndeavors on 2/24/2025 in #help-and-questions
CUID Not Auto-Generating
Yep -- that fixed it! 🙂
13 replies
PPrisma
Created by KinglyEndeavors on 2/24/2025 in #help-and-questions
CUID Not Auto-Generating
Thanks!
13 replies
PPrisma
Created by KinglyEndeavors on 2/24/2025 in #help-and-questions
CUID Not Auto-Generating
Oooooh! Yeah, that makes sense. lol
13 replies
PPrisma
Created by KinglyEndeavors on 2/24/2025 in #help-and-questions
CUID Not Auto-Generating
> node prisma/seed.ts

(node:247163) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
SEEDING DATABASE...
PrismaClientKnownRequestError:
Invalid `prisma.ticket.create()` invocation in
/home/kingofheroes/Projects/playgrounds/react-todo/prisma/seed.ts:49:37

46 data.assignedTo = { connect: { id } };
47 }
48
→ 49 await prisma.ticket.create(
Null constraint violation on the fields: (`_id`)
at Un.handleRequestError (/home/kingofheroes/Projects/playgrounds/react-todo/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:7447)
at Un.handleAndLogRequestError (/home/kingofheroes/Projects/playgrounds/react-todo/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:6771)
at Un.request (/home/kingofheroes/Projects/playgrounds/react-todo/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:6478)
at async l (/home/kingofheroes/Projects/playgrounds/react-todo/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:130:9644)
at async /home/kingofheroes/Projects/playgrounds/react-todo/prisma/seed.ts:49:17
at async Proxy._transactionWithCallback (/home/kingofheroes/Projects/playgrounds/react-todo/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:130:8005)
at async seedDatabase (/home/kingofheroes/Projects/playgrounds/react-todo/prisma/seed.ts:26:9) {
code: 'P2011',
clientVersion: '6.4.1',
meta: { modelName: 'Ticket', constraint: [ '_id' ] }
> node prisma/seed.ts

(node:247163) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
SEEDING DATABASE...
PrismaClientKnownRequestError:
Invalid `prisma.ticket.create()` invocation in
/home/kingofheroes/Projects/playgrounds/react-todo/prisma/seed.ts:49:37

46 data.assignedTo = { connect: { id } };
47 }
48
→ 49 await prisma.ticket.create(
Null constraint violation on the fields: (`_id`)
at Un.handleRequestError (/home/kingofheroes/Projects/playgrounds/react-todo/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:7447)
at Un.handleAndLogRequestError (/home/kingofheroes/Projects/playgrounds/react-todo/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:6771)
at Un.request (/home/kingofheroes/Projects/playgrounds/react-todo/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:6478)
at async l (/home/kingofheroes/Projects/playgrounds/react-todo/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:130:9644)
at async /home/kingofheroes/Projects/playgrounds/react-todo/prisma/seed.ts:49:17
at async Proxy._transactionWithCallback (/home/kingofheroes/Projects/playgrounds/react-todo/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.js:130:8005)
at async seedDatabase (/home/kingofheroes/Projects/playgrounds/react-todo/prisma/seed.ts:26:9) {
code: 'P2011',
clientVersion: '6.4.1',
meta: { modelName: 'Ticket', constraint: [ '_id' ] }
13 replies
PPrisma
Created by KinglyEndeavors on 2/24/2025 in #help-and-questions
CUID Not Auto-Generating
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model User {
id String @id @default(cuid()) @map("_id")
email String @unique @db.VarChar(255)
name String @db.VarChar(255)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp()
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamp()
Ticket Ticket[]

@@index([name])
@@map("users")
}

enum TicketStatus {
OPEN
ASSIGNED
IN_PROGRESS
COMPLETED
}

model Ticket {
id String @id @default(cuid()) @map("_id")
title String @db.VarChar(255)
description String? @db.Text()
assignedTo User? @relation(fields: [id], references: [id], map: "assigned_to", onDelete: Restrict)
status TicketStatus @default(OPEN)
priority Int @default(0) @db.SmallInt()
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp()
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamp()

@@map("tickets")
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model User {
id String @id @default(cuid()) @map("_id")
email String @unique @db.VarChar(255)
name String @db.VarChar(255)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp()
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamp()
Ticket Ticket[]

@@index([name])
@@map("users")
}

enum TicketStatus {
OPEN
ASSIGNED
IN_PROGRESS
COMPLETED
}

model Ticket {
id String @id @default(cuid()) @map("_id")
title String @db.VarChar(255)
description String? @db.Text()
assignedTo User? @relation(fields: [id], references: [id], map: "assigned_to", onDelete: Restrict)
status TicketStatus @default(OPEN)
priority Int @default(0) @db.SmallInt()
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp()
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamp()

@@map("tickets")
}
13 replies
PPrisma
Created by KinglyEndeavors on 2/24/2025 in #help-and-questions
CUID Not Auto-Generating
async function seedDatabase() {
const { range } = require('lodash');
const [{ PrismaClient, TicketStatus }, { faker }] = await Promise.all([
import('@prisma/client'),
import('@faker-js/faker'),
]);

const prisma = new PrismaClient();

try {
const fakeUsers: UserSeed[] = range(10).map(
(): UserSeed =>
({
email: faker.internet.email(),
name: faker.person.fullName(),
} as const),
);

console.log('SEEDING DATABASE...');

await prisma.$connect();
await prisma.$transaction(async () => {
await prisma.ticket.deleteMany();
await prisma.user.deleteMany();

const createdUserIDs: Pick<User, 'id'>[] = await prisma.user.createManyAndReturn({
select: { id: true },
data: fakeUsers,
skipDuplicates: true,
});

for (const _ of range(50)) {
const data: Prisma.TicketCreateInput = {
title: faker.lorem.sentence(),
status: faker.helpers.enumValue(TicketStatus),
priority: faker.number.int({ min: 1, max: 5 }),
description: faker.lorem.paragraph(),
};

if (faker.datatype.boolean()) {
const { id } = faker.helpers.arrayElement(createdUserIDs);
data.assignedTo = { connect: { id } };
}

await prisma.ticket.create({
data,
});
}
});

await prisma.$disconnect();

console.log('DATABASE SEEDING COMPLETE!!!');
process.exit(0);
} catch (error) {
console.error(error);
await prisma.$disconnect();
process.exit(1);
} finally {
await prisma.$disconnect();
}
}
async function seedDatabase() {
const { range } = require('lodash');
const [{ PrismaClient, TicketStatus }, { faker }] = await Promise.all([
import('@prisma/client'),
import('@faker-js/faker'),
]);

const prisma = new PrismaClient();

try {
const fakeUsers: UserSeed[] = range(10).map(
(): UserSeed =>
({
email: faker.internet.email(),
name: faker.person.fullName(),
} as const),
);

console.log('SEEDING DATABASE...');

await prisma.$connect();
await prisma.$transaction(async () => {
await prisma.ticket.deleteMany();
await prisma.user.deleteMany();

const createdUserIDs: Pick<User, 'id'>[] = await prisma.user.createManyAndReturn({
select: { id: true },
data: fakeUsers,
skipDuplicates: true,
});

for (const _ of range(50)) {
const data: Prisma.TicketCreateInput = {
title: faker.lorem.sentence(),
status: faker.helpers.enumValue(TicketStatus),
priority: faker.number.int({ min: 1, max: 5 }),
description: faker.lorem.paragraph(),
};

if (faker.datatype.boolean()) {
const { id } = faker.helpers.arrayElement(createdUserIDs);
data.assignedTo = { connect: { id } };
}

await prisma.ticket.create({
data,
});
}
});

await prisma.$disconnect();

console.log('DATABASE SEEDING COMPLETE!!!');
process.exit(0);
} catch (error) {
console.error(error);
await prisma.$disconnect();
process.exit(1);
} finally {
await prisma.$disconnect();
}
}
13 replies
PPrisma
Created by KinglyEndeavors on 7/3/2024 in #help-and-questions
Best Practices for Error Handling When Updating Bridge Table (Composite IDs)
Looking over the documentation more closely, it does appear to throw an error. Thank you for all your help, nevertheless! I appear to be good now, so again, thank you! 🙂
16 replies
PPrisma
Created by KinglyEndeavors on 7/3/2024 in #help-and-questions
Best Practices for Error Handling When Updating Bridge Table (Composite IDs)
Would it throw an error or will it just return nil?
16 replies
PPrisma
Created by KinglyEndeavors on 7/3/2024 in #help-and-questions
Best Practices for Error Handling When Updating Bridge Table (Composite IDs)
@Olyno Last question: Do I need to confirm the existence of the FacilityWorker entity/record as well before attempting to update it? Or is that handled under the hood?
16 replies
PPrisma
Created by KinglyEndeavors on 7/3/2024 in #help-and-questions
Best Practices for Error Handling When Updating Bridge Table (Composite IDs)
Oh, wait. I see. It was released with 4.7.0 and this project is still using ^4.4.0.
16 replies
PPrisma
Created by KinglyEndeavors on 7/3/2024 in #help-and-questions
Best Practices for Error Handling When Updating Bridge Table (Composite IDs)
It seems to require an array of promises and not just a single promise.
16 replies
PPrisma
Created by KinglyEndeavors on 7/3/2024 in #help-and-questions
Best Practices for Error Handling When Updating Bridge Table (Composite IDs)
@Olyno Is there a reason for it to be returning an array?
16 replies
PPrisma
Created by KinglyEndeavors on 7/3/2024 in #help-and-questions
Best Practices for Error Handling When Updating Bridge Table (Composite IDs)
Gotcha. Thanks. 🙂
16 replies
PPrisma
Created by KinglyEndeavors on 7/3/2024 in #help-and-questions
Best Practices for Error Handling When Updating Bridge Table (Composite IDs)
I guess the part I'm lost on is the fact that the first two first are both read operations that don't update anything that would both throw an error, terminating the flow of operations before reading the actual write operations. Do the read/select queries effect the database in anyway that I'm not considering? Or is it more of a just-in-case anything goes wrong with the actual update/write operation itself?
16 replies
PPrisma
Created by KinglyEndeavors on 7/3/2024 in #help-and-questions
Best Practices for Error Handling When Updating Bridge Table (Composite IDs)
. Also, I understand what $transaction does and what it is for, but why is it necessary within your code sample? Wouldn't the first two checks render that transaction check unneeded? I thought it was just for use cases where you need to upsert multiple models/entities. This isn't me questioning you. This is just me as a Prisma-noob who's seeking to better understand the $transaction feature in this particular use case.
16 replies
PPrisma
Created by KinglyEndeavors on 7/3/2024 in #help-and-questions
Best Practices for Error Handling When Updating Bridge Table (Composite IDs)
Thanks! I figured that may have been the case. but I wasn't 100%. I'm brand new to Prisma (and ORMs in general), and a part of me was thinking that Prisma would already have something like that built in or something. That's not a knock against Prisma or anything; I'm assuming it's just something under-the-hood I'm not aware of.
16 replies