CodeDredd / pinia-orm

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

MorphToMany error when using 'id' on pivot model #1915

Open UncertaintyP opened 3 months ago

UncertaintyP commented 3 months ago

Environment

No response

Reproduction

I am too stupid to use the playground correctly, sorry.

Describe the bug

I tried to implement a Tag model as described in the documentation. Here is the pivot model definition. (Using static primaryKey = 'id' was just for my own sanity ^^)

export default class Taggable extends Model {
  static entity = 'taggables'

  static primaryKey = 'id'

  @Uid() declare id: string

  @Uid() declare tag_id: string

  @Uid() declare taggable_id: string
  @Attr() declare taggable_type: string
}

Adding tags to a model results in the following error

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=f6a016dc:121:9)
    at assert (pinia-orm.js?v=f6a016dc:125:5)
    at Taggable.$getIndexId (pinia-orm.js?v=f6a016dc:3583:5)
    at _Query.reviveOne (pinia-orm.js?v=f6a016dc:1485:27)
    at _Query.reviveRelations (pinia-orm.js?v=f6a016dc:1523:135)
    at _Query.reviveOne (pinia-orm.js?v=f6a016dc:1491:10)
    at pinia-orm.js?v=f6a016dc:1499:26
    at Array.reduce (<anonymous>)
    at _Query.reviveMany (pinia-orm.js?v=f6a016dc:1498:20)
    at _Query.reviveRelations (pinia-orm.js?v=f6a016dc:1523:76)

Looking into the tests I saw the pivot model is defined differently and using this definition does not throw an error

import { Model } from 'pinia-orm'
import { Attr, Uid } from 'pinia-orm/dist/decorators'

export default class Taggable extends Model {
  static entity = 'taggables'

  static primaryKey = ['tag_id', 'taggable_id', 'taggable_type']

  @Uid() declare tag_id: string

  @Uid() declare taggable_id: string
  @Attr() declare taggable_type: string
}

Don't know if that is expected behavior, thus an error in the documentation, or a bug.

Awesome work you are doing here, much love and big thanks. :heart:

Additional context

No response

Logs

No response

CodeDredd commented 1 month ago

This is exspected bahavior because otherwise the pivot cannot have an relation to only one model. Thank's for finding that in the docs. Have to update it.