Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using connect in interactive transaction fails #15044

Closed
steliosrammos opened this issue Aug 27, 2022 · 3 comments · Fixed by #15640
Closed

using connect in interactive transaction fails #15044

steliosrammos opened this issue Aug 27, 2022 · 3 comments · Fixed by #15640
Assignees
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: connect topic: interactiveTransactions topic: prisma-client
Milestone

Comments

@steliosrammos
Copy link

steliosrammos commented Aug 27, 2022

Bug description

Hey, I am trying to run an interactive transaction which has the following sequence:

  1. creates a wallet
  2. creates a wallet link using the wallet's id with a connect statement

The transaction fails with the following error:

An operation failed because it depends on one or more records that were required but not found. No 'Wallet' record(s) (needed to inline the relation on 'WalletLink' record(s)) was found for a nested connect on one-to-many relation 'wallet_linkerTowallet'.

How to reproduce

Here's what the transaction looks like:

  const result = await models.$transaction(async (prisma) => {
      const tempConnectionDetails = await prisma.lndConnectionDetails.create({
        data: {
          hostname,
          grpcPort,
          tlsCertificate: lndNodeType === voltage ? undefined : (tlsCertificate || undefined),
          macaroon: base64macaroon,
          lndNodeType: lndNodeType as LndNodeType,
        },
      });

      const tempWallet = await prisma.wallet.create({
        data: {
          name: name || hostname,
          connectionDetailsType: lnd,
          connectionDetailsId: tempConnectionDetails.id,
          users: {
            create: [{ role: WalletOwner, user: { connect: { id: this.apolloContext.user!.id } } }],
          },
        },
      });

      const walletLink = models.walletLink.create({
        data: {
          wallet: { connect: { id: tempWallet.id } },
          resourceId,
          resourceType: resourceType as WalletResourceType,
        },
      });
      return walletLink;
    });

Note that when printing the tempWallet var in the transaction, it does show the record with an id.

Expected behavior

No response

Prisma information

Relevant part of the schema:

model WalletLink {
  id           BigInt             @id @default(autoincrement())
  resourceId   BigInt             @map("resource_id")
  resourceType WalletResourceType @map("resource_type")
  walletId     BigInt             @map("wallet_id")

  wallet Wallet @relation("wallet_linkTowallet", fields: [walletId], references: [id])

  @@unique([walletId, resourceId, resourceType], name: "uniqueWalletLink")
  @@index([resourceId, resourceType], map: "wallet_link.resource_index")
  @@map("wallet_link")
}

model Wallet {
  id                    BigInt                @id @default(autoincrement())
  name                  String
  connectionDetailsId   BigInt                @map("wallet_id")
  connectionDetailsType ConnectionDetailsType @map("wallet_type")

  users WalletUser[] @relation("wallet_userTowallet")
  links WalletLink[] @relation("wallet_linkTowallet")

  @@map("wallet")
}

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Node.js version: v16.16.0

Prisma Version

prisma                  : 3.15.2
@prisma/client          : 3.15.2
Current platform        : darwin
Query Engine (Node-API) : libquery-engine 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : 461d6a05159055555eb7dfb337c9fb271cbd4d7e
Studio                  : 0.462.0
Preview Features        : interactiveTransactions
@steliosrammos steliosrammos added the kind/bug A reported bug. label Aug 27, 2022
@annibuliful
Copy link

annibuliful commented Aug 29, 2022

I think you should change

const walletLink =await prisma.walletLink.create({
        data: {
          wallet: { connect: { id: tempWallet.id } },
          resourceId,
          resourceType: resourceType as WalletResourceType,
        },
      });

because when you run transaction it if you need primary key or some data from another INSERT/UPDATE from another model in the same transaction, you must use the same transaction id to get them

@garrensmith
Copy link
Contributor

@steliosrammos could you try what @annibuliful has suggested. You need to make sure that all operations are using the transaction client and not the main prisma client.

@garrensmith garrensmith added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. labels Aug 29, 2022
@danstarns
Copy link
Contributor

Added a test for this here: #15640

@steliosrammos

It looks like you are not using the transaction instance inside your transaction for the call walletLink.create. I suggest you use the prisma transaction instance passed into the callback:

- const walletLink = models.walletLink.create({
+ const walletLink = prisma.walletLink.create({
    data: {
      wallet: { connect: { id: tempWallet.id } },
      resourceId,
      resourceType: resourceType as WalletResourceType,
    },
  })
  return walletLink

The tests I have added assert that we can use the connect nested operation inside a transaction, so please could you try again using my suggestion and also with the latest version of Prisma?

@danstarns danstarns self-assigned this Oct 3, 2022
@millsp millsp added kind/bug A reported bug. bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Oct 4, 2022
@janpio janpio added this to the 4.6.0 milestone Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: connect topic: interactiveTransactions topic: prisma-client
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants