Sequelize Data Types: a practical guide
Tags: node, javascript, sequelize, postgres
Published: 2018-12-19 14:31:08

---
title: Sequelize Data Types: a practical guide
published: true
tags: node, javascript, sequelize, postgres 
canonical_url: https://codewithhugo.com/sequelize-data-types-a-practical-guide/
---

DataTypes are core to the Sequelize 4 library. This is a practical and complete guide to make up for the sparse Sequelize official documentation on the subject.

First of all, DataTypes contains both the types and generators for default values: eg. `NOW`, `UUIDV1` and `UUIDV4` are special default values.

The places where you’re likely to encounter DataTypes are in the model field definition and in the migration definitions. Note that for a given model and field, the data type should be the _same_ in the model and in the migration.

Table of contents:

- Text types
- Default values
- Number
- Fancy primitive types
- Date/Time
- Fancy types
- Postgres fancy types

> This is an extract from the [Sequelize ES6 Cheatsheet](https://checkoutpage.co/checkout/hugo/sequelize-cheatsheet)- the better Sequelize docs you wish you had (get it [here]((https://checkoutpage.co/checkout/hugo/sequelize-cheatsheet))).

## Text types

- `STRING`
  - A variable length string.
  - Default length 255.
  - Supports `BINARY`
  - Usage: a 100 length binary string `DataTypes.STRING(100).BINARY`
- `CHAR`
  - A fixed length string.
  - Default length 255.
  - Supports `BINARY`
  - Usage: a 100 length binary char `DataTypes.CHAR(100).BINARY`
- `TEXT`: An unlimited length text column

## Default values

- `NOW`: A default value of the current timestamp
- `UUIDV1`: A default unique universal identifier generated following the UUID v1 standard
- `UUIDV4`: A default unique universal identifier generated following the UUID v2 standard

## Number

All the following support these properties: `UNSIGNED`, `ZEROFILL`.

eg.

```
DataTypes.INTEGER.UNSIGNED.ZEROFILL
// or 
DataTypes.INTEGER.ZEROFILL.UNSIGNED
```

The same can be done using `BIGINT.UNSIGNED`, `FLOAT.UNSIGNED` etc.

- `INTEGER`: A 32 bit integer.
- `BIGINT`: A 64 bit integer.
- `FLOAT`: Floating point number (4-byte precision). Accepts one or two arguments for precision
- `REAL`: Floating point number (4-byte precision). Accepts one or two arguments for precision
- `DOUBLE`: Floating point number (8-byte precision). Accepts one or two arguments for precision
- `DECIMAL`: Decimal number. Accepts one or two arguments for precision

## Fancy primitive types

- `BOOLEAN`: Boolean/tinyint column that gets coerced to a JavaScript Boolean.
- `UUID`: A column storing a unique universal identifier, shape is validated, use with `UUIDV1` or `UUIDV4` default values

## Date/Time

- `TIME`: A time column
- `DATE`: A datetime column
- `DATEONLY`: A date only column

## Fancy types

- `BLOB`: Binary storage. Available lengths: `tiny`, `medium`, `long` eg. `DataTypes.BLOG('tiny')`
- `VIRTUAL`
  - A virtual value that is not stored in the DB. This could for example be useful if you want to provide a default value in your model that is returned to the user but not stored in the DB.
  - [See the docs](https://sequelize.readthedocs.io/en/2.0/api/datatypes/#virtual)
- `ENUM`
  - An enumeration.
  - `DataTypes.ENUM('value', 'another value')`
  - Ideally should be used with strings stored in constants`js
const FIRST_ENUM_VALUE = 'FIRST_ENUM_VALUE';
const OTHER_ENUM_VALUE = 'OTHER_ENUM_VALUE';
// In migration or model definition
DataTypes.ENUM(FIRST_ENUM_VALUE, OTHER_ENUM_VALUE)
`

## Postgres fancy types

- `HSTORE`: A key/value column
- `JSON`: A JSON string column.
- `JSONB`: A pre-processed JSON data column.
- `RANGE`: For Postgres 9.4+, range types are data types representing a range of values of some element type (called the range’s subtype).
- `ARRAY`
  - An array of `type`, e.g. `DataTypes.ARRAY(DataTypes.DECIMAL)`

> This is an extract from the [Sequelize ES6 Cheatsheet](https://checkoutpage.co/checkout/hugo/sequelize-cheatsheet)- the better Sequelize docs you wish you had (get it [here]((https://checkoutpage.co/checkout/hugo/sequelize-cheatsheet))).

[<svg xmlns="http://www.w3.org/2000/svg" style="height:12px;width:auto;position:relative;vertical-align:middle;top:-2px;fill:white" viewbox="0 0 32 32"><title>unsplash-logo</title>
<path d="M10 9V0h12v9H10zm12 5h10v18H0V14h10v9h12v-9z"></path></svg>Mika Baumeister](https://unsplash.com/@mbaumi?utm_medium=referral&utm_campaign=photographer-credit&utm_content=creditBadge "Download free do whatever you want high-resolution photos from Mika Baumeister")