Skip to content

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

@UncertaintyP

Description

@UncertaintyP

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. ❤️

Additional context

No response

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions