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 6 years ago
  • Reactions: 29
  • Comments: 35 (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?

This is one of the most annoying aspects of Prisma. Why does it force me to use connect in some instances but allows me to simply use an id in other instances. Seems pointless.

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

@Poolshark I can’t tell you why, I can just tell you that it’s how types were generated when I checked what Prisma generated to understand the issue.

You can test and see it works.

It’s probably to enforce some kind of consistency, I think it’s actually a good practice

@umussetu and for all writing “same issue” :

This is not an issue per se. If you use “connect” then you should use connect at every step of the current database call that you try to do with Prisma.

If you don’t, then you shouldn’t use it anywhere.

This behavior is enforced into the types generated by Prisma IIRC.

You can’t do :

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

But you can do :

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

or

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

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 😄