question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inheritance + relations

See original GitHub issue

Issue type:

[X] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [X] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[X] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Hello,

I would like to know if their is a possibility to declare a OneToMany relation into an inherited abstract class?

E.g:

BaseUser.ts

import {
    Column,
    OneToMany,
    PrimaryGeneratedColumn,
} from 'typeorm';
import { Token } from './Token';

export abstract class BaseUser {
    @PrimaryGeneratedColumn('uuid')
    public id: string;

    @Column()
    public name?: string;

    @OneToMany(() => Token, (token) => token.user, { onDelete: 'CASCADE' })
    public tokens: Token[];
}

User.ts (Also having the Admin class very similar to the User one)

import {
    Column,
    Entity,
} from 'typeorm';
import UserTypeEnum from '../constants/UserTypeEnum';
import { BaseUser } from './BaseUser';

@Entity()
export class User extends BaseUser {
    @Column({ type: 'simple-array' })
    public roles: string[] = [UserTypeEnum.USER];
}

Token.ts

import {
    Entity, ManyToOne,
    PrimaryGeneratedColumn,
} from 'typeorm';
import { BaseUser } from './BaseUser';
@Entity()

export class Token {
    @PrimaryGeneratedColumn('uuid')
    public id: string;

    @ManyToOne(() => BaseUser, (user) => user.tokens)
    public user: BaseUser;
}

I’m currently getting that error

(node:36999) UnhandledPromiseRejectionWarning: Error: Entity metadata for Token#user was not found. Check if you specified a correct entity object and if it's connected in the connection options.

and I guess this is because I’m using BaseUser into the ManyToOne relation, do you have an idea of how to achieve this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
dawadamcommented, Nov 11, 2020

A solution since last year?

7reactions
mwaeckerlincommented, Oct 28, 2020

Single Table Inheritance is not the answer to this question. I have exactly the same problem:

What I want is Concrete Table Inheritance with relations in the abstract base class.

So at the end, there must be one table with some parts that exist in all three tables, and instead of only simple types, there should be relations.

@ManyToMany works fine, but unfortunately not @OneToMany, because @OneToMany has this useless silly inverse requirement, see also #3233. This is one example, why we rally should have @OneToMany without inverse!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inheritance Relationship - an overview | ScienceDirect Topics
The relationship between a general class and its specializations is known as an inheritance relationship. The inheritance mechanism allows the specialized ...
Read more >
Inheritance (IS-A) vs. Composition (HAS-A) Relationship
IS-A relationship based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. Has-a relationship is composition ...
Read more >
Inheritance: The is-a relation
In programming languages, generalization is denoted by inheritance and/or subtyping. A class C inherits from class D if C has all the data ......
Read more >
Inheritance (object-oriented programming) - Wikipedia
The relationships of objects or classes through inheritance give rise to a directed acyclic graph. An inherited class is called a subclass of...
Read more >
Inheritance
Single-inheritance relationships form tree-like hierarchical structures - a base class exists in a hierarchical relationship with its derived classes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found