# Decorator reference

## Entity decorators[​](#entity-decorators "Direct link to Entity decorators")

#### `@Entity`[​](#entity "Direct link to entity")

Marks your model as an entity. Entity is a class which is transformed into a database table. You can specify the table name in the entity:

```
@Entity("users")

export class User {}
```

This code will create a database table named "users".

You can also specify some additional entity options:

* `name` - table name. If not specified, then table name is generated from entity class name.
* `database` - database name in selected DB server.
* `schema` - schema name.
* `comment` - Database table comment. Not supported by all database types. Currently supported by MySQL, MariaDB, PostgreSQL, SAP HANA.
* `engine` - database engine to be set during table creation (works only in some databases).
* `synchronize` - entities marked with `false` are skipped from schema updates.
* `orderBy` - specifies default ordering for entities when using `find` operations and `QueryBuilder`.

Example:

```
@Entity({

    name: "users",

    engine: "MyISAM",

    database: "example_dev",

    schema: "schema_with_best_tables",

    comment: "This is users table",

    synchronize: false,

    orderBy: {

        name: "ASC",

        id: "DESC",

    },

})

export class User {}
```

Learn more about [Entities](https://typeorm.io/docs/entity/entities.md).

#### `@ViewEntity`[​](#viewentity "Direct link to viewentity")

View entity is a class that maps to a database view.

`@ViewEntity()` accepts following options:

* `name` - view name. If not specified, then view name is generated from entity class name.
* `database` - database name in selected DB server.
* `schema` - schema name.
* `expression` - view definition. **Required parameter**.

`expression` can be string with properly escaped columns and tables, depend on database used (postgres in example):

```
@ViewEntity({

    expression: `

        SELECT "post"."id" "id", "post"."name" AS "name", "category"."name" AS "categoryName"

        FROM "post" "post"

        LEFT JOIN "category" "category" ON "post"."categoryId" = "category"."id"

    `,

})

export class PostCategory {}
```

or an instance of QueryBuilder

```
@ViewEntity({

    expression: (dataSource: DataSource) =>

        dataSource

            .createQueryBuilder()

            .select("post.id", "id")

            .addSelect("post.name", "name")

            .addSelect("category.name", "categoryName")

            .from(Post, "post")

            .leftJoin(Category, "category", "category.id = post.categoryId"),

})

export class PostCategory {}
```

**Note:** parameter binding is not supported due to drivers limitations. Use the literal parameters instead.

```
@ViewEntity({

    expression: (dataSource: DataSource) =>

        dataSource

            .createQueryBuilder()

            .select("post.id", "id")

            .addSelect("post.name", "name")

            .addSelect("category.name", "categoryName")

            .from(Post, "post")

            .leftJoin(Category, "category", "category.id = post.categoryId")

            .where("category.name = :name", { name: "Cars" }) // <-- this is wrong

            .where("category.name = 'Cars'"), // <-- and this is right

})

export class PostCategory {}
```

Learn more about [View Entities](https://typeorm.io/docs/entity/view-entities.md).

## Column decorators[​](#column-decorators "Direct link to Column decorators")

#### `@Column`[​](#column "Direct link to column")

Marks a property in your entity as a table column. Example:

```
@Entity("users")

export class User {

    @Column({ primary: true })

    id: number



    @Column({ type: "varchar", length: 200, unique: true })

    firstName: string



    @Column({ nullable: true })

    lastName: string



    @Column({ default: false })

    isActive: boolean

}
```

`@Column` accept several options you can use:

* `type: ColumnType` - Column type. One of the [supported column types](https://typeorm.io/docs/entity/entities.md#column-types).
* `name: string` - Column name in the database table. By default, the column name is generated from the name of the property. You can change it by specifying your own name.
* `length: string|number` - Column type's length. For example, if you want to create `varchar(150)` type you specify column type and length options.
* `width: number` - column type's display width. Used only for [MySQL integer types](https://dev.mysql.com/doc/refman/5.7/en/integer-types.html). *Deprecated* in newer MySQL versions, will be removed from TypeORM in an upcoming version.
* `onUpdate: string` - `ON UPDATE` trigger. Used only in [MySQL](https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html).
* `nullable: boolean` - determines whether the column can become `NULL` or always has to be `NOT NULL`. By default column is `nullable: false`.
* `update: boolean` - Indicates if column value is updated by "save" operation. If false, you'll be able to write this value only when you first time insert the object. Default value is `true`.
* `insert: boolean` - Indicates if column value is set the first time you insert the object. Default value is `true`.
* `select: boolean` - Defines whether or not to hide this column by default when making queries. When set to `false`, the column data will not show with a standard query. By default column is `select: true`
* `default: string` - Adds database-level column's `DEFAULT` value.
* `primary: boolean` - Marks column as primary. Same as using `@PrimaryColumn`.
* `unique: boolean` - Marks column as unique column (creates unique constraint). Default value is false.
* `comment: string` - Database's column comment. Not supported by all database types.
* `precision: number` - The precision for a decimal (exact numeric) column (applies only for decimal column), which is the maximum number of digits that are stored for the values. Used in some column types.
* `scale: number` - The scale for a decimal (exact numeric) column (applies only for decimal column), which represents the number of digits to the right of the decimal point and must not be greater than precision. Used in some column types.
* `zerofill: boolean` - Puts `ZEROFILL` attribute on to a numeric column. Used only in MySQL. If `true`, MySQL automatically adds the `UNSIGNED` attribute to this column. *Deprecated* in newer MySQL versions, will be removed from TypeORM in an upcoming version. Use a character column and the `LPAD` function as suggested by MySQL.
* `unsigned: boolean` - Puts `UNSIGNED` attribute on to a numeric column. Used only in MySQL.
* `charset: string` - Defines a column character set. Not supported by all database types.
* `collation: string` - Defines a column collation.
* `enum: string[]|AnyEnum` - Used in `enum` column type to specify list of allowed enum values. You can specify array of values or specify a enum class.
* `enumName: string` - A name for generated enum type. If not specified, TypeORM will generate a enum type from entity and column names - so it's necessary if you intend to use the same enum type in different tables.
* `primaryKeyConstraintName: string` - A name for the primary key constraint. If not specified, then constraint name is generated from the table name and the names of the involved columns.
* `asExpression: string` - Generated column expression. Supported by PostgreSQL/CockroachDB, MySQL/MariaDB, Oracle, SAP HANA, Spanner, SQLite, and SQL Server.
* `generatedType: "VIRTUAL"|"STORED"` - Generated column type. Supported by PostgreSQL (`STORED` only), CockroachDB, MySQL/MariaDB, Oracle (`VIRTUAL` only), Spanner (`STORED` only), SQLite, and SQL Server (`STORED` only, internally mapped to `PERSISTED`).
* `hstoreType: "object"|"string"` - Return type of `HSTORE` column. Returns value as string or as object. Used only in [Postgres](https://www.postgresql.org/docs/9.6/static/hstore.html).
* `array: boolean` - Used for postgres and cockroachdb column types which can be array (for example int\[]).
* `transformer: ValueTransformer|ValueTransformer[]` - Specifies a value transformer (or array of value transformers) that is to be used to (un)marshal this column when reading or writing to the database. In case of an array, the value transformers will be applied in the natural order from entityValue to databaseValue, and in reverse order from databaseValue to entityValue.
* `spatialFeatureType: string` - Optional feature type (`Point`, `Polygon`, `LineString`, `Geometry`) used as a constraint on a spatial column. If not specified, it will behave as though `Geometry` was provided. Used only in PostgreSQL and CockroachDB.
* `srid: number` - Optional [Spatial Reference ID](https://postgis.net/docs/using_postgis_dbmanagement.html#spatial_ref_sys) used as a constraint on a spatial column. If not specified, it will default to `0`. Standard geographic coordinates (latitude/longitude in the WGS84 datum) correspond to [EPSG 4326](http://spatialreference.org/ref/epsg/wgs-84/). Used only in PostgreSQL and CockroachDB.

Learn more about [entity columns](https://typeorm.io/docs/entity/entities.md#entity-columns).

#### `@PrimaryColumn`[​](#primarycolumn "Direct link to primarycolumn")

Marks a property in your entity as a table primary column. Same as `@Column` decorator but sets its `primary` option to true.

Example:

```
@Entity()

export class User {

    @PrimaryColumn()

    id: number

}
```

`@PrimaryColumn()` supports custom primary key constraint name:

```
@Entity()

export class User {

    @PrimaryColumn({ primaryKeyConstraintName: "pk_user_id" })

    id: number

}
```

> Note: when using `primaryKeyConstraintName` with multiple primary keys, the constraint name must be the same for all primary columns.

Learn more about [entity columns](https://typeorm.io/docs/entity/entities.md#entity-columns).

#### `@PrimaryGeneratedColumn`[​](#primarygeneratedcolumn "Direct link to primarygeneratedcolumn")

Marks a property in your entity as a table-generated primary column. Column it creates is primary and its value is auto-generated. Example:

```
@Entity()

export class User {

    @PrimaryGeneratedColumn()

    id: number

}
```

`@PrimaryGeneratedColumn()` supports custom primary key constraint name:

```
@Entity()

export class User {

    @PrimaryGeneratedColumn({ primaryKeyConstraintName: "pk_user_id" })

    id: number

}
```

There are four generation strategies:

* `increment` - uses AUTO\_INCREMENT / SERIAL / SEQUENCE (depend on database type) to generate incremental number.

* `identity` - only for [PostgreSQL 10+](https://www.postgresql.org/docs/13/sql-createtable.html). Postgres versions above 10 support the SQL-Compliant **IDENTITY** column. When marking the generation strategy as `identity` the column will be produced using `GENERATED [ALWAYS|BY DEFAULT] AS IDENTITY`

* `uuid` - generates unique `uuid` string.

* `rowid` - only for [CockroachDB](https://www.cockroachlabs.com/docs/stable/serial.html). Value is automatically generated using the `unique_rowid()` function. This produces a 64-bit integer from the current timestamp and ID of the node executing the `INSERT` or `UPSERT` operation.

  <!-- -->

  > Note: property with a `rowid` generation strategy must be a `string` data type

Default generation strategy is `increment`, to change it to another strategy, simply pass it as the first argument to decorator:

```
@Entity()

export class User {

    @PrimaryGeneratedColumn("uuid")

    id: string

}
```

Learn more about [entity columns](https://typeorm.io/docs/entity/entities.md#entity-columns).

#### `@ObjectIdColumn`[​](#objectidcolumn "Direct link to objectidcolumn")

Marks a property in your entity as `ObjectId`. This decorator is only used in MongoDB. Every entity in MongoDB must have an `ObjectId` column. Example:

```
@Entity()

export class User {

    @ObjectIdColumn()

    id: ObjectId

}
```

Learn more about [MongoDB](https://typeorm.io/docs/drivers/mongodb.md).

#### `@CreateDateColumn`[​](#createdatecolumn "Direct link to createdatecolumn")

Special column that is automatically set to the entity's insertion time. You don't need to write a value into this column - it will be automatically set. Example:

```
@Entity()

export class User {

    @CreateDateColumn()

    createdDate: Date

}
```

#### `@UpdateDateColumn`[​](#updatedatecolumn "Direct link to updatedatecolumn")

Special column that is automatically set to the entity's update time each time you call `save` from entity manager or repository. You don't need to write a value into this column - it will be automatically set.

This column is also automatically updated during `upsert` operations when an update occurs due to a conflict.

```
@Entity()

export class User {

    @UpdateDateColumn()

    updatedDate: Date

}
```

#### `@DeleteDateColumn`[​](#deletedatecolumn "Direct link to deletedatecolumn")

Special column that is automatically set to the entity's delete time each time you call soft-delete of entity manager or repository. You don't need to set this column - it will be automatically set.

TypeORM's own soft delete functionality utilizes global scopes to only pull "non-deleted" entities from the database.

If the @DeleteDateColumn is set, the default scope will be "non-deleted".

```
@Entity()

export class User {

    @DeleteDateColumn()

    deletedDate: Date

}
```

#### `@VersionColumn`[​](#versioncolumn "Direct link to versioncolumn")

Special column that is automatically set to the entity's version (incremental number) each time you call `save` from entity manager or repository. You don't need to write a value into this column - it will be automatically set.

This column is also automatically updated during `upsert` operations when an update occurs due to a conflict.

```
@Entity()

export class User {

    @VersionColumn()

    version: number

}
```

#### `@Generated`[​](#generated "Direct link to generated")

Marks column to be a generated value. For example:

```
@Entity()

export class User {

    @Column()

    @Generated("uuid")

    uuid: string

}
```

Value will be generated only once, before inserting the entity into the database.

#### `@VirtualColumn`[​](#virtualcolumn "Direct link to virtualcolumn")

Special column that is never saved to the database and thus acts as a readonly property. Each time you call `find` or `findOne` from the entity manager, the value is recalculated based on the query function that was provided in the VirtualColumn Decorator. The alias argument passed to the query references the exact entity alias of the generated query behind the scenes.

```
@Entity({ name: "companies", alias: "COMP" })

export class Company extends BaseEntity {

    @PrimaryColumn("varchar", { length: 50 })

    name: string



    @VirtualColumn({

        query: (alias) =>

            `SELECT COUNT("name") FROM "employees" WHERE "companyName" = ${alias}.name`,

    })

    totalEmployeesCount: number



    @OneToMany((type) => Employee, (employee) => employee.company)

    employees: Employee[]

}



@Entity({ name: "employees" })

export class Employee extends BaseEntity {

    @PrimaryColumn("varchar", { length: 50 })

    name: string



    @ManyToOne((type) => Company, (company) => company.employees)

    company: Company

}
```

## Relation decorators[​](#relation-decorators "Direct link to Relation decorators")

#### `@OneToOne`[​](#onetoone "Direct link to onetoone")

One-to-one is a relation where A contains only one instance of B, and B contains only one instance of A. Let's take for example `User` and `Profile` entities. User can have only a single profile, and a single profile is owned by only a single user. Example:

```
import { Entity, OneToOne, JoinColumn } from "typeorm"

import { Profile } from "./Profile"



@Entity()

export class User {

    @OneToOne((type) => Profile, (profile) => profile.user)

    @JoinColumn()

    profile: Profile

}
```

Learn more about [one-to-one relations](https://typeorm.io/docs/relations/one-to-one-relations.md).

#### `@ManyToOne`[​](#manytoone "Direct link to manytoone")

Many-to-one / one-to-many is a relation where A contains multiple instances of B, but B contains only one instance of A. Let's take for example `User` and `Photo` entities. User can have multiple photos, but each photo is owned by only one single user. Example:

```
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from "typeorm"

import { User } from "./User"



@Entity()

export class Photo {

    @PrimaryGeneratedColumn()

    id: number



    @Column()

    url: string



    @ManyToOne((type) => User, (user) => user.photos)

    user: User

}
```

Learn more about [many-to-one / one-to-many relations](https://typeorm.io/docs/relations/many-to-one-one-to-many-relations.md).

#### `@OneToMany`[​](#onetomany "Direct link to onetomany")

Many-to-one / one-to-many is a relation where A contains multiple instances of B, but B contains only one instance of A. Let's take for example `User` and `Photo` entities. User can have multiple photos, but each photo is owned by only a single user. Example:

```
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from "typeorm"

import { Photo } from "./Photo"



@Entity()

export class User {

    @PrimaryGeneratedColumn()

    id: number



    @Column()

    name: string



    @OneToMany((type) => Photo, (photo) => photo.user)

    photos: Photo[]

}
```

Learn more about [many-to-one / one-to-many relations](https://typeorm.io/docs/relations/many-to-one-one-to-many-relations.md).

#### `@ManyToMany`[​](#manytomany "Direct link to manytomany")

Many-to-many is a relation where A contains multiple instances of B, and B contain multiple instances of A. Let's take for example `Question` and `Category` entities. Question can have multiple categories, and each category can have multiple questions. Example:

```
import {

    Entity,

    PrimaryGeneratedColumn,

    Column,

    ManyToMany,

    JoinTable,

} from "typeorm"

import { Category } from "./Category"



@Entity()

export class Question {

    @PrimaryGeneratedColumn()

    id: number



    @Column()

    title: string



    @Column()

    text: string



    @ManyToMany((type) => Category)

    @JoinTable()

    categories: Category[]

}
```

Learn more about [many-to-many relations](https://typeorm.io/docs/relations/many-to-many-relations.md).

#### `@JoinColumn`[​](#joincolumn "Direct link to joincolumn")

Defines which side of the relation contains the join column with a foreign key and allows you to customize the join column name, referenced column name and foreign key name. Example:

```
@Entity()

export class Post {

    @ManyToOne((type) => Category)

    @JoinColumn({

        name: "cat_id",

        referencedColumnName: "name",

        foreignKeyConstraintName: "fk_cat_id",

    })

    category: Category

}
```

#### `@JoinTable`[​](#jointable "Direct link to jointable")

Used for `many-to-many` relations and describes join columns of the "junction" table. Junction table is a special, separate table created automatically by TypeORM with columns referenced to the related entities. You can change the name of the generated "junction" table, the column names inside the junction table, their referenced columns with the `joinColumn`- and `inverseJoinColumn` attributes, and the created foreign keys names. You can also set parameter `synchronize` to false to skip schema update(same way as in @Entity)

Example:

```
@Entity()

export class Post {

    @ManyToMany((type) => Category)

    @JoinTable({

        name: "question_categories",

        joinColumn: {

            name: "question",

            referencedColumnName: "id",

            foreignKeyConstraintName: "fk_question_categories_questionId",

        },

        inverseJoinColumn: {

            name: "category",

            referencedColumnName: "id",

            foreignKeyConstraintName: "fk_question_categories_categoryId",

        },

        synchronize: false,

    })

    categories: Category[]

}
```

If the destination table has composite primary keys, then an array of properties must be sent to the `@JoinTable` decorator.

#### `@RelationId`[​](#relationid "Direct link to relationid")

Loads id (or ids) of specific relations into properties. For example, if you have a many-to-one `category` in your `Post` entity, you can have a new category id by marking a new property with `@RelationId`. Example:

```
@Entity()

export class Post {

    @ManyToOne((type) => Category)

    category: Category



    @RelationId((post: Post) => post.category) // you need to specify target relation

    categoryId: number

}
```

This functionality works for all kind of relations, including `many-to-many`:

```
@Entity()

export class Post {

    @ManyToMany((type) => Category)

    categories: Category[]



    @RelationId((post: Post) => post.categories)

    categoryIds: number[]

}
```

Relation id is used only for representation. The underlying relation is not added/removed/changed when chaining the value.

## Subscriber and listener decorators[​](#subscriber-and-listener-decorators "Direct link to Subscriber and listener decorators")

#### `@AfterLoad`[​](#afterload "Direct link to afterload")

You can define a method with any name in entity and mark it with `@AfterLoad` and TypeORM will call it each time the entity is loaded using `QueryBuilder` or repository/manager find methods. Example:

```
@Entity()

export class Post {

    @AfterLoad()

    updateCounters() {

        if (this.likesCount === undefined) this.likesCount = 0

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@BeforeInsert`[​](#beforeinsert "Direct link to beforeinsert")

You can define a method with any name in entity and mark it with `@BeforeInsert` and TypeORM will call it before the entity is inserted using repository/manager `save`. Example:

```
@Entity()

export class Post {

    @BeforeInsert()

    updateDates() {

        this.createdDate = new Date()

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@AfterInsert`[​](#afterinsert "Direct link to afterinsert")

You can define a method with any name in entity and mark it with `@AfterInsert` and TypeORM will call it after the entity is inserted using repository/manager `save`. Example:

```
@Entity()

export class Post {

    @AfterInsert()

    resetCounters() {

        this.counters = 0

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@BeforeUpdate`[​](#beforeupdate "Direct link to beforeupdate")

You can define a method with any name in the entity and mark it with `@BeforeUpdate` and TypeORM will call it before an existing entity is updated using repository/manager `save`. Example:

```
@Entity()

export class Post {

    @BeforeUpdate()

    updateDates() {

        this.updatedDate = new Date()

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@AfterUpdate`[​](#afterupdate "Direct link to afterupdate")

You can define a method with any name in the entity and mark it with `@AfterUpdate` and TypeORM will call it after an existing entity is updated using repository/manager `save`. Example:

```
@Entity()

export class Post {

    @AfterUpdate()

    updateCounters() {

        this.counter = 0

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@BeforeRemove`[​](#beforeremove "Direct link to beforeremove")

You can define a method with any name in the entity and mark it with `@BeforeRemove` and TypeORM will call it before an entity is removed using repository/manager `remove`. Example:

```
@Entity()

export class Post {

    @BeforeRemove()

    updateStatus() {

        this.status = "removed"

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@AfterRemove`[​](#afterremove "Direct link to afterremove")

You can define a method with any name in the entity and mark it with `@AfterRemove` and TypeORM will call it after the entity is removed using repository/manager `remove`. Example:

```
@Entity()

export class Post {

    @AfterRemove()

    updateStatus() {

        this.status = "removed"

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@BeforeSoftRemove`[​](#beforesoftremove "Direct link to beforesoftremove")

You can define a method with any name in the entity and mark it with `@BeforeSoftRemove` and TypeORM will call it before an entity is soft removed using repository/manager `softRemove`. Example:

```
@Entity()

export class Post {

    @BeforeSoftRemove()

    updateStatus() {

        this.status = "soft-removed"

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@AfterSoftRemove`[​](#aftersoftremove "Direct link to aftersoftremove")

You can define a method with any name in the entity and mark it with `@AfterSoftRemove` and TypeORM will call it after the entity is soft removed using repository/manager `softRemove`. Example:

```
@Entity()

export class Post {

    @AfterSoftRemove()

    updateStatus() {

        this.status = "soft-removed"

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@BeforeRecover`[​](#beforerecover "Direct link to beforerecover")

You can define a method with any name in the entity and mark it with `@BeforeRecover` and TypeORM will call it before an entity is recovered using repository/manager `recover`. Example:

```
@Entity()

export class Post {

    @BeforeRecover()

    updateStatus() {

        this.status = "recovered"

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@AfterRecover`[​](#afterrecover "Direct link to afterrecover")

You can define a method with any name in the entity and mark it with `@AfterRecover` and TypeORM will call it after the entity is recovered using repository/manager `recover`. Example:

```
@Entity()

export class Post {

    @AfterRecover()

    updateStatus() {

        this.status = "recovered"

    }

}
```

Learn more about [listeners](https://typeorm.io/docs/listeners-and-subscribers.md).

#### `@EventSubscriber`[​](#eventsubscriber "Direct link to eventsubscriber")

Marks a class as an event subscriber which can listen to specific entity events or any entity's events. Events are fired using `QueryBuilder` and repository/manager methods. Example:

```
@EventSubscriber()

export class PostSubscriber implements EntitySubscriberInterface<Post> {

    /**

     * Indicates that this subscriber only listen to Post events.

     */

    listenTo() {

        return Post

    }



    /**

     * Called before post insertion.

     */

    beforeInsert(event: InsertEvent<Post>) {

        console.log(`BEFORE POST INSERTED: `, event.entity)

    }

}
```

You can implement any method from `EntitySubscriberInterface`. To listen to any entity, you just omit the `listenTo` method and use `any`:

```
@EventSubscriber()

export class PostSubscriber implements EntitySubscriberInterface {

    /**

     * Called before entity insertion.

     */

    beforeInsert(event: InsertEvent<any>) {

        console.log(`BEFORE ENTITY INSERTED: `, event.entity)

    }

}
```

Learn more about [subscribers](https://typeorm.io/docs/listeners-and-subscribers.md).

## Other decorators[​](#other-decorators "Direct link to Other decorators")

#### `@Index`[​](#index "Direct link to index")

This decorator allows you to create a database index for a specific column or columns. It also allows you to mark column or columns to be unique. This decorator can be applied to columns or an entity itself. Use it on a column when an index on a single column is needed and use it on the entity when a single index on multiple columns is required. Examples:

```
@Entity()

export class User {

    @Index()

    @Column()

    firstName: string



    @Index({ unique: true })

    @Column()

    lastName: string

}
```

```
@Entity()

@Index(["firstName", "lastName"])

@Index(["lastName", "middleName"])

@Index(["firstName", "lastName", "middleName"], { unique: true })

export class User {

    @Column()

    firstName: string



    @Column()

    lastName: string



    @Column()

    middleName: string

}
```

Learn more about [indexes](https://typeorm.io/docs/indexes.md).

#### `@Unique`[​](#unique "Direct link to unique")

This decorator allows you to create a database unique constraint for a specific column or columns. This decorator can be applied only to an entity itself. You must specify the entity field names (not database column names) as arguments.

Examples:

```
@Entity()

@Unique(["firstName"])

@Unique(["lastName", "middleName"])

@Unique("UQ_NAMES", ["firstName", "lastName", "middleName"])

export class User {

    @Column({ name: "first_name" })

    firstName: string



    @Column({ name: "last_name" })

    lastName: string



    @Column({ name: "middle_name" })

    middleName: string

}
```

> Note: MySQL stores unique constraints as unique indexes

#### `@Check`[​](#check "Direct link to check")

This decorator allows you to create a database check constraint for a specific column or columns. This decorator can be applied only to an entity itself.

Examples:

```
@Entity()

@Check(`"firstName" <> 'John' AND "lastName" <> 'Doe'`)

@Check(`"age" > 18`)

export class User {

    @Column()

    firstName: string



    @Column()

    lastName: string



    @Column()

    age: number

}
```

> Note: MySQL does not support check constraints.

#### `@Exclusion`[​](#exclusion "Direct link to exclusion")

This decorator allows you to create a database exclusion constraint for a specific column or columns. This decorator can be applied only to an entity itself.

Examples:

```
@Entity()

@Exclusion(`USING gist ("room" WITH =, tsrange("from", "to") WITH &&)`)

export class RoomBooking {

    @Column()

    room: string



    @Column()

    from: Date



    @Column()

    to: Date

}
```

> Note: Only PostgreSQL supports exclusion constraints.

#### `@ForeignKey`[​](#foreignkey "Direct link to foreignkey")

This decorator allows you to create a database foreign key for a specific column or columns. This decorator can be applied to columns or an entity itself. Use it on a column when an foreign key on a single column is needed and use it on the entity when a single foreign key on multiple columns is required.

> Note: **Do not use this decorator with relations.** Foreign keys are created automatically for relations which you define using [Relation decorators](#relation-decorators) (`@ManyToOne`, `@OneToOne`, etc). The `@ForeignKey` decorator should only be used to create foreign keys in the database when you don't want to define an equivalent entity relationship.

Examples:

```
@Entity("orders")

@ForeignKey(() => City, ["cityId", "countryCode"], ["id", "countryCode"])

export class Order {

    @PrimaryColumn()

    id: number



    @Column("uuid", { name: "user_uuid" })

    @ForeignKey<User>("User", "uuid", { name: "FK_user_uuid" })

    userUuid: string



    @Column({ length: 2 })

    @ForeignKey(() => Country, "code")

    countryCode: string



    @Column()

    @ForeignKey("cities")

    cityId: number



    @Column()

    dispatchCountryCode: string



    @ManyToOne(() => Country)

    dispatchCountry: Country



    @Column()

    dispatchCityId: number



    @ManyToOne(() => City)

    dispatchCity: City

}
```

```
@Entity("cities")

@Unique(["id", "countryCode"])

export class City {

    @PrimaryColumn()

    id: number



    @Column({ length: 2 })

    @ForeignKey("countries", { onDelete: "CASCADE", onUpdate: "CASCADE" })

    countryCode: string



    @Column()

    name: string

}
```

```
@Entity("countries")

export class Country {

    @PrimaryColumn({ length: 2 })

    code: string



    @Column()

    name: string

}
```

```
@Entity("users")

export class User {

    @PrimaryColumn({ name: "ref" })

    id: number



    @Column("uuid", { unique: true })

    uuid: string

}
```
