Skip to content

Single Table Inheritance with types never returns from inherited tables but only from the base entity #1631

@tintin10q

Description

@tintin10q

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 typeKey = "person_type"
  static types() {
    return {
      PERSON: Person,
      ADULT: Adult,
    };
  }
  static fields() {
    return {
      id: this.attr(null),
      name: this.attr(""),
      person_type: this.string("No type given?"),
    };
  }
}
// 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>
  <br>
  <br>
  <button
    @click="
      person.insert([
        { person_type: 'PERSON', id: 1, name: 'John Doe' },
        { person_type: 'ADULT', id: 2, name: 'Jane Doe', job: 'Software Engineer' },
      ])
    "
  >
    Insert Person and adult through person
  </button>
  <br>
  <br>
  <button
    @click="
      adult.insert([
        // { person_type: 'PERSON', id: 1, name: 'John Doe' },
        { person_type: 'ADULT', id: 2, name: 'Jane Doe', job: 'Software Engineer' },
      ])
    "
  >
    Insert adult
  </button>
  <h2>Adults</h2>
  <div v-for="a in all_adults">- {{ a }} </div>

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

Describe the bug

  1. Click the Insert Person and adult through person button
  2. Observe that only the people section gets populated but not the adults

image

I would expect that the adults list would also be populated.

Even when you try to insert adults directly through the adults model it still does not show up on the page.

Additional context

The person table does work as expected which is really great! It returns all the persons including the adults. The adults have the specific adult fields and the persons do not. So the bug is that the other models do not seem to be populated.

This also effects other models that have relationships with the sub models of person. So like if I would have a drink model which has a one to many relationship with adults called liked_by then this never gets populated. I left that out for the minimum example.

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