question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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”

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
benhicksoncommented, May 29, 2021

@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?

1reaction
janpiocommented, May 31, 2021

Let’s update the issue title then 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Relations (Reference) - Prisma
Relation fields define connections between models at the Prisma level and do not ... In SQL, you use a foreign key to create...
Read more >
GraphQL schema basics
This article describes the fundamental building blocks of a schema and how to create one for your GraphQL server. The schema definition language....
Read more >
API (GraphQL) - Data modeling - AWS Amplify Docs
You need to explicitly specify the connection field names if relational directives are used to create two connections of the same type between...
Read more >
NEXT VALUE FOR (Transact-SQL) - Microsoft Learn
For a complete discussion of both creating and using sequences, ... The NEXT VALUE FOR function cannot be used in the following situations:....
Read more >
Documentation: 15: SELECT - PostgreSQL
If both clauses are used, the column added by the SEARCH clause appears before the columns added by the CYCLE clause. The primary...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found