CodeDredd / pinia-orm

The Pinia plugin to enable Object-Relational Mapping access to the Pinia Store.
https://pinia-orm.codedredd.de/
MIT License
453 stars 39 forks source link

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

Open martinszeltins opened 1 year ago

martinszeltins commented 1 year ago

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:  pnpm@8.1.0
- Builder:          -
- User Config:      devtools, modules
- Runtime Modules:  @pinia/nuxt@0.4.11, @pinia-plugin-persistedstate/nuxt@1.1.1, @pinia-orm/nuxt@1.6.7
- 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>


### Additional context

_No response_

### Logs

_No response_
CodeDredd commented 1 year ago

@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 commented 1 year ago

@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

Sayanel34 commented 1 year ago

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

ChrizzDF commented 1 year ago

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

Sayanel34 commented 1 year ago

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 commented 1 year ago

Did someone figure something out by any chance ?

mariusgnicula commented 1 year ago

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 commented 1 year ago

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