What’s wrong on this connect? User already exists...
# orm-help
p
What’s wrong on this connect? User already exists
Copy code
const website = await prisma.website.create({
      data: {
        name: name,
        domain: domain,

        users: {
          connect: {
            userId: userId,
            role: 'owner'
          }
        },
      },
    })
a
Hey @Paulo Castellano , I don’t see anything wrong immediately, but it would help if you could share the
schema.prisma
and any errors you are seeing.
p
Hey @Austin model Website { id String @id @default(uuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt name String @db.VarChar(255) domain String @db.VarChar(255) logo String? @db.VarChar(255) users _WebsiteUser_[] } model User { id String @id @default(uuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt email String @unique name String password String websites _WebsiteUser_[] } model WebsiteUser { user User @relation(_fields_: [userId], _references_: [id]) userId String website Website @relation(_fields_: [websiteId], _references_: [id]) websiteId String role String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@id([userId, websiteId]) }
👋 1
i gotting this error:
Copy code
const website = await Kernel.prisma.website.create({
       data: {
         name: 'Google',
         domain: '<http://google.com|google.com>',
         users: {
           connect: {
             userId: '7b766d35-7cd9-4c39-860e-b71b4ceb680e',
             ~~~~~~
             role: 'owner'
             ~~~~
           }
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         }
       }
     })

Argument data.users.connect of type WebsiteUserWhereUniqueInput needs exactly one argument, but you provided userId and role. Please choose one. Available args: 
type WebsiteUserWhereUniqueInput {
  userId_websiteId?: WebsiteUserUserIdWebsiteIdCompoundUniqueInput
}
but i need pass the role do pivot table
a
The
WebsiteUser
model has a composite ID of
userId
and
websiteId
, so that’s what you have to pass in order to
connect
the record. If you want to update the
role
, that would happen in a subsequent
update
call, possible in a transaction.
p
@Austin its possible just create a website, and in the next query only connect?
example:
Copy code
await prisma.website.update({
      where: {
        id: website.id
      },
      data: {
        users: {
          connect: {
            id: request.user.id,
            role: 'owner'
          },
        },
      }
    })
@Austin this way works for me, but i dont know if thats correct way to work with prismajs:
Copy code
// get user
const user = await prisma.user.findUnique({
    where: {
    id: request.user.id,
    },
})

// create website
const website = await prisma.website.create({
    data: {
    name: name,
    domain: domain
    },
})

await prisma.WebsiteUser.create({
    data: {
    websiteId: website.id,
    userId: request.user.id,
    role: 'owner'
    },
});
a
The above code should work fine, though I would wrap it in a transaction. May I ask if you are using Prisma in a work project or personal project?
p
@Austin i starting a new side project
the relation works, but i cannot take all websites from the user =/
the docs of prisma its very low =/
a
Could you clarify a bit further what the issue is? Could we improve our docs somewhere?