Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
UncertaintyP opened this issue Aug 5, 2024 · 1 comment
Open

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

UncertaintyP opened this issue Aug 5, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@UncertaintyP
Copy link

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

@CodeDredd CodeDredd added documentation Improvements or additions to documentation and removed pending triage labels Sep 14, 2024
@CodeDredd
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants