Create 2 entities and connect in postgesql. relat...
# orm-help
y
Create 2 entities and connect in postgesql. relations. 1. account can have multiple jobs (
@relation("accountsTojobs_account_id")
in schema.prisma ) 2. account has one
create_job_id
(
@relation("accounts_create_job_idTojobs", fields: [create_job_id], references: [id])
in schema.prisma ) I'm trying to create a job -> an account according to prisma client relation queries documentation, when i create a
job
and include
account
( code below ), i shouldn't have to specify
create_job_id
in account object since i won't know the id until its creation. but the vs code throws an error saying
create_job_id
is required field. Error Message
Copy code
Type '{ email: string; }' is not assignable to type 'Without<AccountUncheckedCreateWithoutJobsInput, AccountCreateWithoutJobsInput> & AccountCreateWithoutJobsInput'.
my gut tells me i'm missing something on schema file. Could anyone take a look at this and give me some feedback? Thanks in advance, schema.prisma
Copy code
model Account {
  id                       Int           @id @default(autoincrement())
  email                 String
  create_job_id            Int
  create_account_job       Job           @relation("accounts_create_job_idTojobs", fields: [create_job_id], references: [id])
  jobs                     Job[]         @relation("accountsTojobs_account_id")

  @@map("accounts")
}

model Job {
  id             Int       @id @default(autoincrement())
  title          String?
  status         String    @default("pending")
  account_id     Int?
  account        Account?  @relation("accountsTojobs_account_id", fields: [account_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "jobs_account_id_fkey")
  accounts       Account[] @relation("accounts_create_job_idTojobs")

  @@map("jobs")
}
code : account create object throws an error unless i add create_job_id which i won't know until parent(job) is created
Copy code
prisma.job.create({
      data: {
        title: `Create an account`,
        status: `PENDING`,
        account: {
          create: {
            email: `<mailto:hello@test.com|hello@test.com>`,
            // create_job_id : ??? i shouldn't have to add this line
          },
        },
      },
    });
h
Hey there 👋 I have't used a NoSQL database inside of Prisma before, so I'm not sure how relations work in the -- although from a quick glance I believe the issue here might be the fact that
create_job_id
isn't an index 😄
y
it looks like I had to change from
Copy code
account: {
    create: {
        email: `<mailto:hello@test.com|hello@test.com>`,
        // create_job_id : ??? i shouldn't have to add this line
    },
},
to
Copy code
accounts: {
    create: [{
        email: `<mailto:hello@test.com|hello@test.com>`,
        // now i don't need to add create_job_id
    }],
},
fast parrot 1
h
Glad that worked for you. I was mainly looking at what could be wrong within your Prisma schema itself since you said that's where you believed the error was. But cheers on solving the problem.
y
yeah, i thought it was schema file but turned out it was code part. still i appreciate you looking into it! 👍