Prisma Create with Connect Issue
Hey!
I wasn't able to find another thread explaining this, but I'd love help with this!
I have two tables in my database right now, they are Org and OrgType (in Prisma). Here is how they look right now:
I'm using the T3 stack going off the Chirp tutorial video (I'm a noob at js), and I'm trying to insert into the Org table, which has the orgTypeId that I need to insert (this is selected via a dropdown on the client side).
Any help would be greatly appreciated!
model Org {
id String @id @default(cuid())
albatrossOrgId Int @map("albatross_org_id") //albatross org id
name String
nickname String?
parentOrgId String? @map("parent_org_id")
orgType OrgType @relation(references: [id], fields: [orgTypeId])
orgTypeId String @map("org_type_id") //scalar fields above
// Data Upkeep
createdBy String? @map("created_by")
modifiedBy String? @map("modified_by")
createdDate DateTime @default(now()) @map("created_date")
modifiedDate DateTime? @updatedAt @map("modified_date")
activeFlag Boolean @default(true) @map("active_flag")
archiveFlag Boolean @default(false) @map("archive_flag")
// Relational Fields
// Relational Indexes
@@index([orgTypeId])
// Naming
@@map("org")
}
model OrgType {
id String @id @default(cuid())
name String
parentTypeId String? @map("parent_type_id")
// Data Upkeep
createdBy String? @map("created_by")
modifiedBy String? @map("modified_by")
createdDate DateTime @default(now()) @map("created_date")
modifiedDate DateTime? @updatedAt @map("modified_date")
activeFlag Boolean @default(true) @map("active_flag")
archiveFlag Boolean @default(false) @map("archive_flag")
// Relational Fields
Org Org[]
// Relational Indexes
// Naming
@@map("org_type")
}
model Org {
id String @id @default(cuid())
albatrossOrgId Int @map("albatross_org_id") //albatross org id
name String
nickname String?
parentOrgId String? @map("parent_org_id")
orgType OrgType @relation(references: [id], fields: [orgTypeId])
orgTypeId String @map("org_type_id") //scalar fields above
// Data Upkeep
createdBy String? @map("created_by")
modifiedBy String? @map("modified_by")
createdDate DateTime @default(now()) @map("created_date")
modifiedDate DateTime? @updatedAt @map("modified_date")
activeFlag Boolean @default(true) @map("active_flag")
archiveFlag Boolean @default(false) @map("archive_flag")
// Relational Fields
// Relational Indexes
@@index([orgTypeId])
// Naming
@@map("org")
}
model OrgType {
id String @id @default(cuid())
name String
parentTypeId String? @map("parent_type_id")
// Data Upkeep
createdBy String? @map("created_by")
modifiedBy String? @map("modified_by")
createdDate DateTime @default(now()) @map("created_date")
modifiedDate DateTime? @updatedAt @map("modified_date")
activeFlag Boolean @default(true) @map("active_flag")
archiveFlag Boolean @default(false) @map("archive_flag")
// Relational Fields
Org Org[]
// Relational Indexes
// Naming
@@map("org_type")
}
1 Reply
Here is what my create procedure looks like right now:
create: privateProcedure
.input(
orgSchema
)
.mutation(async ({ ctx, input }) => {
const currentUser = ctx.userId
// Must pass the ratelimit
const { success } = await ratelimit.limit(currentUser)
if (!success) throw new TRPCError({ code: "TOO_MANY_REQUESTS" });
const org = await ctx.prisma.org.create({
data: {
createdBy: currentUser,
modifiedBy: currentUser,
name: input.name,
nickname: input.nickname,
albatrossOrgId: input.albatrossOrgId,
parentOrgId: input.parentOrgId,
// orgType: {
// connect: {id: input.orgTypeId}
// }
},
});
return org;
}),
create: privateProcedure
.input(
orgSchema
)
.mutation(async ({ ctx, input }) => {
const currentUser = ctx.userId
// Must pass the ratelimit
const { success } = await ratelimit.limit(currentUser)
if (!success) throw new TRPCError({ code: "TOO_MANY_REQUESTS" });
const org = await ctx.prisma.org.create({
data: {
createdBy: currentUser,
modifiedBy: currentUser,
name: input.name,
nickname: input.nickname,
albatrossOrgId: input.albatrossOrgId,
parentOrgId: input.parentOrgId,
// orgType: {
// connect: {id: input.orgTypeId}
// }
},
});
return org;
}),