Tengo un pequeño problema con la tabla de datos relacionales, aquí está mi modelo y la pregunta está debajo de esto.
model User { id String @id @default(dbgenerated()) @map("_id") @db.ObjectId createdAt DateTime @default(now()) updatedAt DateTime @updatedAt email String @unique firstName String ... rsvpedSessions Session[] @relation("Rsvped", fields: [rsvpedSessionId]) rsvpedSessionId String[] @db.Array(ObjectId) notRespondedSessions Session[] @relation("notResponded", fields: [notRespondedSessionId]) notRespondedSessionId String[] @db.Array(ObjectId) declinedSessions Session[] @relation("Declined", fields: [declinedSessionId]) declinedSessionId String[] @db.Array(ObjectId) model Session { id String @id @default(dbgenerated()) @map("_id") @db.ObjectId createdAt DateTime @default(now()) updatedAt DateTime @updatedAt title String ... rsvpedUsers User[] @relation("Rsvped", fields: [rsvpedUserId]) rsvpedUserId String[] @db.Array(ObjectId) notRespondedUsers User[] @relation("notResponded", fields: [notRespondedUserId]) notRespondedUserId String[] @db.Array(ObjectId) declinedUsers User[] @relation("Declined", fields: [declinedUserId]) declinedUserId String[] @db.Array(ObjectId)cuando creo la sesión agregando el usuario a notResponded. Puedo ver al usuario en la matriz notResponded pero, en User, no puedo obtener la sesión en notRespondedSessions. Simplemente vincula al usuario a la sesión, pero no vincula la sesión al usuario.
Creo la sesión con
return await prisma.session.create({ data: { ...args, notRespondedUserId: args.userID, }, });y un detalle más, cuando intento conectarme en notRespondedUsers, dice que connect no es un argumento válido.
tenes idea de cual puede ser el motivo?
Si desea crear una sesión y conectarla a un registro de user existente, debe usar la API de connect con el campo de relación en cuestión (usuarios no notRespondedUsers ) en lugar de intentar asignar un valor directamente a ID de notRespondedUserId .
Creo que esta es la consulta que estás buscando:
await prisma.session.create({ data: { ...args notRespondedUsers: { connect: { id: args.userID // if there is more than one user, you can also pass an array of { id: string} objects to connect } } }, }); Puede encontrar la sintaxis y los detalles para connect en los documentos de Prisma .