> For the complete documentation index, see [llms.txt](https://orkhan.gitbook.io/typeorm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://orkhan.gitbook.io/typeorm/docs/docs/entity/3-entity-inheritance.md).

# Entity Inheritance

## Concrete Table Inheritance

You can reduce duplication in your code by using entity inheritance patterns.\
The simplest and the most effective is concrete table inheritance.

For example, you have `Photo`, `Question`, `Post` entities:

```typescript
@Entity()
export class Photo {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string

    @Column()
    size: string
}
```

```typescript
@Entity()
export class Question {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string

    @Column()
    answersCount: number
}
```

```typescript
@Entity()
export class Post {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string

    @Column()
    viewCount: number
}
```

As you can see all those entities have common columns: `id`, `title`, `description`.\
To reduce duplication and produce a better abstraction we can create a base class called `Content` for them:

```typescript
export abstract class Content {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string
}
```

```typescript
@Entity()
export class Photo extends Content {
    @Column()
    size: string
}
```

```typescript
@Entity()
export class Question extends Content {
    @Column()
    answersCount: number
}
```

```typescript
@Entity()
export class Post extends Content {
    @Column()
    viewCount: number
}
```

All columns (relations, embeds, etc.) from parent entities (parent can extend other entity as well)\
will be inherited and created in final entities.

This example will create 3 tables - `photo`, `question` and `post`.

## Single Table Inheritance

TypeORM also supports single table inheritance.\
Single table inheritance is a pattern when you have multiple classes with their own properties,\
but in the database they are stored in the same table.

```typescript
@Entity()
@TableInheritance({ column: { type: "varchar", name: "type" } })
export class Content {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string
}
```

```typescript
@ChildEntity()
export class Photo extends Content {
    @Column()
    size: string
}
```

```typescript
@ChildEntity()
export class Question extends Content {
    @Column()
    answersCount: number
}
```

```typescript
@ChildEntity()
export class Post extends Content {
    @Column()
    viewCount: number
}
```

This will create a single table called `content` and all instances of photos, questions and posts\
will be saved into this table.

## Using embeddeds

There is an amazing way to reduce duplication in your app (using composition over inheritance) by using `embedded columns`.\
Read more about embedded entities [here](/typeorm/docs/docs/entity/2-embedded-entities.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://orkhan.gitbook.io/typeorm/docs/docs/entity/3-entity-inheritance.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
