Skip to content

Single Table Inheritance with types() static method without exposing typeKey gives error #1630

@tintin10q

Description

@tintin10q

I took the example from the docs but it gives an error.

Reproduction

<script setup lang="ts">
import { computed } from "vue";

import { Model, useRepo } from "pinia-orm";

// Base entity.
class Person extends Model {
  static entity = "person";
  static types() {
    return {
      PERSON: Person,
      ADULT: Adult,
    };
  }
  static fields() {
    return {
      id: this.attr(null),
      name: this.attr(""),
      // type: this.string("No type given?"), // uncomment to make error go away
    };
  }
}
// Derived entity.
class Adult extends Person {
  static entity = "adult";
  static baseEntity = "person";
  static fields() {
    return {
      ...super.fields(),
      job: this.attr(""),
    };
  }
}
const adult = useRepo(Adult);
const person = useRepo(Person);


const all_persons = computed(() => person.all());
const all_adults = computed(() => adult.all());

function clear() {
  adult.flush();
  person.flush()
}

</script>

<template>
  <button @click="clear">Clear</button>
  <h2>Adults</h2>
  <span v-for="a in all_adults">- {{ a }} </span>

  <h2>Persons</h2>
  <span v-for="p in all_persons">- {{ p }} </span>
</template>

Describe the bug

  1. Try to load the component and observe the error

If you expose the type field by adding type: this.string("No type given?"), to the fields method of Person the error goes away. If you set the static typeKey then you have to expose the correct name for the error to go away.

This is a bug because the docs claim you can not expose the type key if you want to. But I don't really see why you would not want to expose it.

The error also goes away if you remove the baseEntity field from Adult.

Additional context

I put this component in the playground and I got the same error as in my own project.

Logs

Uncaught (in promise) TypeError: this.model.$fields()[this.model.$typeKey()] is undefined
    internalGet index.mjs:868
    get index.mjs:848
    all index.mjs:1564
    all_adults TestInheritBugType.vue:38
    run reactivity.esm-bundler.js:178
    get value reactivity.esm-bundler.js:1147
    unref reactivity.esm-bundler.js:1026
    get reactivity.esm-bundler.js:1032
    _sfc_render TestInheritBugType.vue:43
    renderComponentRoot runtime-core.esm-bundler.js:816
    componentUpdateFn runtime-core.esm-bundler.js:5701
    run reactivity.esm-bundler.js:178
    update runtime-core.esm-bundler.js:5814
    setupRenderEffect runtime-core.esm-bundler.js:5822
    mountComponent runtime-core.esm-bundler.js:5612

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