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

Relations don't use namespace to make/retrieve related model #1740

Closed alvaro-canepa closed 6 months ago

alvaro-canepa commented 1 year ago

Environment

pinia-orm: 1.7.2 Vite: 4.5.0 Vue: 3.3.6 Typescript: 5.2.2

Reproduction

Main User model

// models/main/User.ts
import { Model } from 'pinia-orm'

export default class User extends Model {
  static entity = 'users';
  static namespace = 'main';

  static fields() {
    return {
      id: this.uid(),
      name: this.string(''),
      age: this.number(null),
    }
  }
}

Extended User model from another namespace

// models/extended/User.ts
import { Model } from 'pinia-orm';
import MainUser from '@/models/main/User';

export default class User extends Model {
  static entity = 'users';
  static namespace = 'extended';

  static fields() {
    return {
      id: this.uid(),
      user_id: this.attr(''),
      user: this.belongsTo(MainUser, 'user_id'),
//      ... more fields
    }
  }
}

Make relation

import User from '@/models/extended/User';

const user = useRepo(User).make({
    user: {
      name: 'John Doe',
      age: 30
    }
});

Describe the bug

When make a model with related one as same entity but different namespace, results was a related model as a same of parent (like it was category and child category).

To solve this, entity must be unique per model. If I not wrong, related models must use $storeName() instead of $entity().

Additional context

No response

Logs

No response

CodeDredd commented 10 months ago

You are right. Trying to reproduce it and making a fix.