Unique constraint failed on the constraint: `PRIMARY` composite key

62 views Asked by At

I have Prisma/MySQL model with a composite key. When a new record is created, the record is created but Prisma gives the error: Unique constraint failed on the constraint: PRIMARY.

Here is the model:

    model shop_session {
      shop_id          BigInt
      user_id          BigInt
      myshopify_domain String    @db.VarChar(255)
      access_token     String    @db.VarChar(64)
      first_name       String    @db.VarChar(50)
      last_name        String    @db.VarChar(50)
      email            String    @db.VarChar(255)
      account_owner    Boolean   @default(false)
      locale           String    @db.VarChar(10)
      collaborator     Boolean   @default(false)
      email_verified   Boolean   @default(false)
      scope            String    @db.VarChar(255)
      ip               String    @db.VarChar(50)
      isp              String    @db.VarChar(100)
      ua               String    @db.VarChar(100)
      os               String    @db.VarChar(100)
      created_at       DateTime  @db.DateTime(0)
      updated_at       DateTime? @db.DateTime(0)
      expires_at       DateTime  @db.DateTime(0)
    
      @@id([shop_id, user_id])
    }

The code:

export async function createUserSession (shop_id, myshopify, authorization) {

  const expires_in = authorization.expires_in;
  const currentTime = Date.now();
  const expires_at = new Date(currentTime + (expires_in * 1000));

  const upsert = await db.shop_session.upsert({
    where: {
      shop_id_user_id: {
        shop_id: shop_id,
        user_id: authorization.associated_user.id,
      },
    },
    update: {
      myshopify_domain: myshopify,
      access_token: authorization.access_token,
      first_name: authorization.associated_user.first_name,
      last_name: authorization.associated_user.last_name,
      email: authorization.associated_user.email,
      account_owner: authorization.associated_user.account_owner,
      locale: authorization.associated_user.locale,
      collaborator: authorization.associated_user.collaborator,
      email_verified: authorization.associated_user.email_verified,
      scope: authorization.scope,
      ip: '',
      isp: '',
      ua: '',
      os: '',
      updated_at: new Date(),
      expires_at: expires_at
    },
    create: {
      shop_id: shop_id,
      user_id: authorization.associated_user.id,
      myshopify_domain: myshopify,
      access_token: authorization.access_token,
      first_name: authorization.associated_user.first_name,
      last_name: authorization.associated_user.last_name,
      email: authorization.associated_user.email,
      account_owner: authorization.associated_user.account_owner,
      locale: authorization.associated_user.locale,
      collaborator: authorization.associated_user.collaborator,
      email_verified: authorization.associated_user.email_verified,
      scope: authorization.scope,
      ip: '',
      isp: '',
      ua: '',
      os: '',
      created_at: new Date(),
      updated_at: new Date(),
      expires_at: expires_at
    }
  });

  return upsert;
}

The error:

PrismaClientKnownRequestError: 
Invalid `db.shop_session.upsert()` invocation in
/Users/asacarter/Shopify/auth2/app/models/shop.server.js:133:40

  130 const futureTime = currentTime + (seconds * 1000); // Converts the seconds to milliseconds and adds to the current time
  131 const expires_at = new Date(futureTime);
  132 
→ 133 const upsert = await db.shop_session.upsert(
Unique constraint failed on the constraint: `PRIMARY`
    at wn.handleRequestError (/Users/asacarter/Shopify/auth2/node_modules/@prisma/client/runtime/library.js:123:6730)
    at wn.handleAndLogRequestError (/Users/asacarter/Shopify/auth2/node_modules/@prisma/client/runtime/library.js:123:6119)
    at wn.request (/Users/asacarter/Shopify/auth2/node_modules/@prisma/client/runtime/library.js:123:5839)
    at l (/Users/asacarter/Shopify/auth2/node_modules/@prisma/client/runtime/library.js:128:9763)
    at createUserSession (/Users/asacarter/Shopify/auth2/app/models/shop.server.js:133:18)
    at Authenticator (/Users/asacarter/Shopify/auth2/app/services/auth.server.js:188:18)
    at loader2 (/Users/asacarter/Shopify/auth2/app/routes/_index.jsx:11:14)
    at Object.callRouteLoaderRR (/Users/asacarter/Shopify/auth2/node_modules/@remix-run/server-runtime/dist/data.js:52:16)
    at callLoaderOrAction (/Users/asacarter/Shopify/auth2/node_modules/@remix-run/router/router.ts:3671:16)
    at async Promise.all (index 1)
0

There are 0 answers