Column options for the model schema attributes. Used in Model.init and Sequelize#define, and the Attribute decorator.

interface AttributeOptions<M> {
    _autoGenerated?: boolean;
    allowNull?: boolean;
    autoIncrement?: boolean;
    autoIncrementIdentity?: boolean;
    columnName?: string;
    comment?: string;
    defaultValue?: unknown;
    field?: string;
    get?: ((this) => unknown);
    index?: AllowArray<string | boolean | AttributeIndexOptions>;
    onDelete?: ReferentialAction;
    onUpdate?: ReferentialAction;
    primaryKey?: boolean;
    references?: string | ModelStatic<Model<any, any>> | AttributeReferencesOptions;
    set?: ((this, val) => void);
    type: DataType;
    unique?: AllowArray<string | boolean | {
        msg?: string;
        name: string;
    }>;
    validate?: ColumnValidateOptions;
}

Type Parameters

Hierarchy (view full)

Properties

_autoGenerated?: boolean

This attribute is added by sequelize. Do not use!

allowNull?: boolean

If false, the column will have a NOT NULL constraint, and a not null validation will be run before an instance is saved.

Default

true
autoIncrement?: boolean

Is this field an auto increment field

autoIncrementIdentity?: boolean

If this field is a Postgres auto increment field, use Postgres GENERATED BY DEFAULT AS IDENTITY instead of SERIAL. Postgres 10+ only.

columnName?: string

The name of the column.

If no value is provided, Sequelize will use the name of the attribute (in snake_case if InitOptions.underscored is true)

comment?: string

Comment to add on the column in the database.

defaultValue?: unknown

A literal default value, a JavaScript function, or an SQL function (using fn)

field?: string

Deprecated

use columnName instead.

get?: ((this) => unknown)

Provide a custom getter for this column. Use Model.getDataValue to access the underlying values.

Type declaration

    • (this): unknown
    • Parameters

      • this: M

      Returns unknown

index?: AllowArray<string | boolean | AttributeIndexOptions>

If true, an index will be created for this column. If a string is provided, the column will be part of a composite index together with the other attributes that specify the same index name.

What should happen when the referenced key is deleted. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION

What should happen when the referenced key is updated. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION

primaryKey?: boolean

If true, this attribute will be marked as primary key

references?: string | ModelStatic<Model<any, any>> | AttributeReferencesOptions

Makes this attribute a foreign key. You typically don't need to use this yourself, instead use associations.

Setting this value to a string equivalent to setting it to { tableName: 'myString' }.

set?: ((this, val) => void)

Provide a custom setter for this column. Use Model.setDataValue to access the underlying values.

Type declaration

    • (this, val): void
    • Parameters

      • this: M
      • val: any

      Returns void

type: DataType

A string or a data type.

unique?: AllowArray<string | boolean | {
    msg?: string;
    name: string;
}>

If true, the column will get a unique constraint. If a string is provided, the column will be part of a composite unique index. If multiple columns have the same string, they will be part of the same unique index

An object of validations to execute for this column every time the model is saved. Can be either the name of a validation provided by validator.js, a validation function provided by extending validator.js (see the DAOValidator property for more details), or a custom validation function. Custom validation functions are called with the value of the field, and can possibly take a second callback argument, to signal that they are asynchronous. If the validator is sync, it should throw in the case of a failed validation, it is async, the callback should be called with the error text.