pinia-orm icon indicating copy to clipboard operation
pinia-orm copied to clipboard

Error: [Pinia ORM] The record is missing the primary key.

Open martinszeltins opened this issue 1 year ago • 8 comments

Environment

- Operating System: Ubuntu 20.04 LTS (Linux)
- Node Version:     v18.12.1
- Nuxt Version:     3.7.1
- CLI Version:      3.7.3
- Nitro Version:    2.6.2
- Package Manager:  [email protected]
- Builder:          -
- User Config:      devtools, modules
- Runtime Modules:  @pinia/[email protected], @pinia-plugin-persistedstate/[email protected], @pinia-orm/[email protected]
- Build Modules:    -

Reproduction

https://github.com/martinszeltins/pinia-orm-new

Describe the bug

I am getting this error:

Uncaught Error: [Pinia ORM] The record is missing the primary key. If you want to persist record without the primary key, please define the primary key field with the `uid` attribute.
    at throwError (pinia-orm.js?v=65a69f6d:104:9)
    at assert (pinia-orm.js?v=65a69f6d:108:5)
    at User.$getIndexId (pinia-orm.js?v=65a69f6d:3054:5)
    at pinia-orm.js?v=65a69f6d:1567:21
    at Array.reduce (<anonymous>)
    at _Query.compile (pinia-orm.js?v=65a69f6d:1566:23)
    at _Query.insert (pinia-orm.js?v=65a69f6d:1426:32)
    at Repository.insert (pinia-orm.js?v=65a69f6d:1898:25)
    at addUser (app.vue:11:16)
    at callWithErrorHandling (vue.js?v=2c56b3b3:1565:18)

I have a very simple setup as you can tell from my reproduction repo. I just followed the docs.

  1. I created user.ts Model:
import { Model } from 'pinia-orm'
import { Str, Uid } from 'pinia-orm/dist/decorators'

export default class User extends Model {
  static entity = 'users'
  
  @Uid() declare id: string
  @Str('') declare name: string
  @Str('') declare email: string
}
  1. And this is my usage just like in the docs:
<template>
  <div>
    {{ users }}
  </div>

  <div>
    <button @click="addUser">
        Add user
    </button>
  </div>
</template>

<script setup lang="ts">
    import User from './stores/models/user'
    import { useRepo } from 'pinia-orm'

    const userRepo = useRepo(User)

    // Getting all users
    const users = userRepo.all()

    const addUser = () => {
        userRepo.insert({
            name: 'John Doe',
            email: '[email protected]'
        })
    }
</script>

Additional context

No response

Logs

No response

martinszeltins avatar Sep 06 '23 06:09 martinszeltins

@martinszeltins Thanks for the report. Don't know yet why this error happans. With nuxt 3.6.5 this works. So somehow nuxt 3.7.1 still has somthing wrong.

Update: Issue opened https://github.com/nuxt/nuxt/issues/23038

CodeDredd avatar Sep 06 '23 08:09 CodeDredd

@martinszeltins Ok after some investigating i found a workaround for nuxt 3.7.1 . You can look there: https://github.com/nuxt/nuxt/issues/23038#issuecomment-1710268991

CodeDredd avatar Sep 07 '23 14:09 CodeDredd

Still have this issue with a Many to Many relationship, no matter what i'm doing with vite and nuxt versions.

Sayanel34 avatar Oct 27 '23 12:10 Sayanel34

Just an FYI: It's also not solved by using Nuxt 3.8.0

ChrizzDF avatar Oct 30 '23 12:10 ChrizzDF

Yeah i didn't try it since i had other problems with this new version, but thanks for testing it. I didn't try anything since my first post, i'll prob work on it within the week since it's blocking anyway.

Sayanel34 avatar Oct 30 '23 12:10 Sayanel34

Did someone figure something out by any chance ?

Sayanel34 avatar Nov 07 '23 07:11 Sayanel34

Nuxt 3.8.1 sadly doesn't have a fix for this either. The solution described here https://github.com/nuxt/nuxt/issues/23038 seems to be the only workaround right now.

mariusgnicula avatar Nov 17 '23 19:11 mariusgnicula

Hey fellas! So I got this working with Nuxt 3.8.1 and Vite 5.0.0. Apparently it wasn't enough to add experimental decorators to my TS config, but add it to Nuxt config as well, like so:

export default defineNuxtConfig({
  debug: true,
  devtools: { enabled: true },
  modules: ['@pinia/nuxt', '@pinia-orm/nuxt'],
  typescript: {
    typeCheck: true,
    strict: true,
  },
  vite: {
    esbuild: {
      tsconfigRaw: {
        compilerOptions: {
          experimentalDecorators: true,
        }
      }
    }
  }
})

Got this workaround from here: https://github.com/vitejs/vite/issues/13736

mariusgnicula avatar Nov 19 '23 20:11 mariusgnicula