prisma: When using a `connect` or `create` block in a `create()` or `update()`, other scalar relation fields cannot be used directly

Bug description

I have a model with multiple relations to single entities. When I try to .create with this model, I have the option of providing those relations via passing the IDs of the foreign entities to the _id columns, or I can use Prisma’s relations to perform a connect: {}.

However, when I want to perform a nested write, creating one of the related entities when I create the main entity, I am not able to pass the ID to create the other relation - prisma requires me to use the connect: {} method instead.

This feels like a bug.

Apologies if this is already an Issue, I’m not really sure how to articulate a search for this.

How to reproduce

Given this schema:

model MainItem {
  id                  Int            @id @default(autoincrement())
  related_item_one_id Int
  related_item_one    RelatedItemOne @relation("main_item__to__related_item_one", fields: [related_item_one_id], references: [id])
  related_item_two_id Int
  related_item_two    RelatedItemTwo @relation("main_item__to__related_item_two", fields: [related_item_two_id], references: [id])
}

model RelatedItemOne {
  id       Int        @id @default(autoincrement())
  api_keys MainItem[] @relation("main_item__to__related_item_one")
}

model RelatedItemTwo {
  id       Int        @id @default(autoincrement())
  api_keys MainItem[] @relation("main_item__to__related_item_two")
}

This works fine:

prisma.mainItem.create({
  data: {
    related_item_one_id: 123,
    related_item_two_id: 456,
  }
})

However, this throws a TypeError:

prisma.mainItem.create({
  data: {
    related_item_one_id: 123, 
    related_item_two: {
      connect: {
        id: 456
      }
    },
  }
})

// Types of property 'related_item_one_id' are incompatible.
//   Type 'number' is not assignable to type 'undefined'. ts(2322)

That example is somewhat less useful though than my current use case: performing a nested write to create the related entity. This works fine:

prisma.mainItem.create({
  data: {
    related_item_one: {
      connect: {
        id: 123
      }
    },
    related_item_two: {
      create: {}
    },
  }
})

However, this throws the TypeError:

prisma.mainItem.create({
  data: {
    related_item_one_id: 123,
    related_item_two: {
      create: {}
    },
  }
})

// Types of property 'related_item_one_id' are incompatible.
//   Type 'number' is not assignable to type 'undefined'. ts(2322)

Expected behavior

I would expect these more terse versions to be valid.

Prisma information

(see above)

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Node.js version: “12.20.1”
  • Prisma version: “2.21.2”

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 15
  • Comments: 22 (3 by maintainers)

Most upvoted comments

@Karl-EdwardFPJeanMehu Thanks for the reply!

Putting nested writes to the side for a moment, and merely creating a record with already-existing foreign keys, both of these are valid:

prisma.mainItem.create({
  data: {
    related_item_one_id: 123,
    related_item_two_id: 456,
  }
})
prisma.mainItem.create({
  data: {
    related_item_one: {
      connect: {
        id: 123
      }
    },
    related_item_two: {
      connect: {
        id: 456
      }
    },
  }
})

Yet, this version that includes one relation via the connect {} block, and one through directly passing the id, is invalid.

prisma.mainItem.create({
  data: {
    related_item_one: {
      connect: {
        id: 123
      }
    },
    related_item_two_id: 456
  }
})

What’s the functional difference between these 3 versions? Shouldn’t all 3 be valid?

I am running into the same issue. the following create function shows my issue. i really have no clue how to get out of it. normally i could easily just use connect instead of id in all places and it would work. but since connect cannot handle nulls (https://github.com/prisma/prisma/issues/2271), i am lost…

 db.item.create({
    relation: {  // i need to create a relation here
      create: {
         relation_item_one_id: null, // needs to be null, since it otherwise defaults to a specified value, but actually i would need to use a connect here, because of before mentioned reasons... 
         relation_item_two: {  // i need to use connect because i am using nested create
          connect: {
            id: '123
          }
         }
      }
     }
 })

and the reason why i have to set a default on the relation_item_one_id, is actually Row-Level-Security. It is my organisation id and matches these patterns: https://github.com/prisma/prisma/issues/12735

Bump! This is really annoying :[

@MatiasDuhalde is null the database default? in that case why set it at all? just allow it to default

Let’s update the issue title then 😄