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.

Planetscale not able to upsert with Prisma

See original GitHub issue

Bug description

It seems like the upsert command will yield HY000 code 1105 error.

const response = await prisma.user.update({
      data: {
        profile: {
          upsert: {
            update: {
              firstName: req.body.profile.firstName,
              lastName: req.body.profile.lastName,
            },
            create: {
              firstName: 'GG',
              lastName: 'lala',
            }
          }
        }
      },
      where: {
        id: token.sub
      }
    })
model Profile {
  id        String  @id @default(cuid())
  // BUG: https://github.com/prisma/prisma/issues/10758#issuecomment-1001139677
  // Need to use onUpdate: Noaction according to 10758
  user      User    @relation(fields: [userId], references: [id], onUpdate: Cascade, onDelete: Cascade)
  userId    String  @unique
  timezone  String?
  company   String?
  jobTitle  String?
  firstName String?
  lastName  String?
}
prisma:info Starting a mysql pool with 3 connections.
prisma:query BEGIN
prisma:query SELECT `silkroad-db`.`User`.`id` FROM `silkroad-db`.`User` WHERE `silkroad-db`.`User`.`id` = ?
prisma:query SELECT `silkroad-db`.`StakeholdersOnIssues`.`userId`, `silkroad-db`.`StakeholdersOnIssues`.`issueId` FROM `silkroad-db`.`StakeholdersOnIssues` WHERE (1=1 AND `silkroad-db`.`StakeholdersOnIssues`.`userId` IN (?))
prisma:query SELECT `silkroad-db`.`User`.`id`, `silkroad-db`.`StakeholdersOnIssues`.`userId`, `silkroad-db`.`StakeholdersOnIssues`.`issueId` FROM `silkroad-db`.`StakeholdersOnIssues` WHERE 1=0
prisma:query ROLLBACK
error - Error: 
Invalid `prisma.user.update()` invocation:


  Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Server(ServerError { code: 1105, message: "symbol `silkroad-db`.`User`.id not found", state: "HY000" })) })
wait  - compiling /_error (client and server)...

How to reproduce

Expected behavior

Prisma information

Environment & setup

  • OS:
  • Database:
  • Node.js version:

Prisma Version


Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
chrisyeung1121commented, Jan 28, 2022

The workaround works for now! @janpio thank you.

So for now - the rule of thumb is all the relations to the model that you update should have onUpdate:NoAction and onDelete:NoAction.

And the impact will be there will be if “parent ID” updates or get deleted, the related model will not cascade automatically.

0reactions
timotius02commented, Mar 27, 2022

I’m running into this as well but the workaround has not worked for me.

This what my schema looks like:

model Video {
  id String @id
  lastUpdated DateTime @updatedAt
  name String
  viewCount String
  thumbnail String
  playlists VideosOnPlaylists[]
}

model VideosOnPlaylists {
  videoId String
  video Video @relation(fields: [videoId], references: [id], onUpdate: NoAction, onDelete: NoAction) 
  playlistId Int
  playlist Playlist @relation(fields: [playlistId], references: [id], onUpdate: NoAction, onDelete: NoAction)

  @@id([videoId, playlistId])
}

model Playlist {
  id Int @id @default(autoincrement())
  youtubeId String?
  name String
  createdAt DateTime
  thumbnail String
  type PLAYLIST_TYPE
  videos VideosOnPlaylists[]
}

enum PLAYLIST_TYPE {
  PLAYLIST
  CHANNEL
}

As you can see I have a many to many relations and have set the referrential actions to no actions.

 prisma.video.upsert({
  where: { id: item.id },
  update: {
    viewCount: item.statistics.viewCount,
    playlists: {
      create: {
        playlistId: playlist.id,
      },
    },
  },
  create: {
    id: item.id,
    name: item.snippet.title,
    thumbnail: thumbnails.standard
      ? thumbnails.standard.url
      : thumbnails.high.url,
    viewCount: item.statistics.viewCount,
    playlists: {
      create: {
        playlistId: playlist.id,
      },
    },
  },
})

This is the query I’m trying to run for reference.

ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Server(ServerError { code: 1105, message: "symbol views_game.Video.id not found", state: "HY000" })) })

as well as the error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Prisma with PlanetScale
Prisma recommends not using prisma migrate when making schema changes with PlanetScale. Instead, we recommend that you use the prisma db push command....
Read more >
Prisma with PlanetScale quickstart
Overview. This guide will show you how to: Create a database with PlanetScale; Integrate into a Next.js + Prisma application. Prerequisites.
Read more >
PlanetScale and Prisma Data Platform integration
Overview. The following guide will show you how to integrate PlanetScale with a Prisma application using the Prisma Data Platform integration.
Read more >
How to set up Next.js with Prisma and PlanetScale
On the free plan, you receive one database at no cost to you. Create a new database and select the closest region to...
Read more >
Automatic Prisma migrations
Also, you previously needed to turn on the ability to automatically copy the Prisma migration metadata. You no longer need to do this....
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