# Fields, Types, Nullability and Defaults — Prisma / TypeORM / Sequelize

Source: https://www.skillbyai.com/en/orms/or-fields-types

> Declare columns with the right type, nullability, defaults, unique constraints, indexes and timestamps in Prisma, TypeORM and Sequelize.

## Every field is a column decision

Each field you add to a model becomes a column, and four decisions travel with it. **Type**: the ORM maps a language type such as `string` to a database type such as `text` or `varchar`; you can usually override it when you need precision, for example a fixed-point decimal for money instead of a floating-point number. **Nullability**: Prisma marks optional fields with `?`, TypeORM uses `nullable: true`, Sequelize uses `allowNull`. Prisma fields are required by default; TypeORM columns are `NOT NULL` by default; in Sequelize `allowNull` defaults to true, which surprises many people. **Defaults**: values the database fills in, such as the current timestamp. **Uniqueness and indexes**: `@unique` or `unique: true` create a constraint, and explicit indexes speed up lookups on columns you filter by often, such as foreign keys. Getting these right in the model means the database itself rejects bad data.

## From model to table

A model declares fields; fields become typed columns; relation fields become foreign keys; together they produce the final table definition.

![Four connected stages: a model definition, its typed columns, foreign key columns for relations, and the resulting database table.](assets/figures/orms/section-3-map.svg) — Figure 3.1: model -> columns -> foreign keys -> table

## The same Profile in Prisma and TypeORM

Optional bio, default role, automatic timestamps. In TypeORM a union type like `string | null` needs an explicit column `type`, because decorator metadata cannot describe a union.

```ts
// Prisma (schema.prisma)
// model Profile {
//   id        Int      @id @default(autoincrement())
//   handle    String   @unique
//   bio       String?
//   role      String   @default("student")
//   createdAt DateTime @default(now())
//   updatedAt DateTime @updatedAt
// }

// TypeORM
@Entity()
export class Profile {
  @PrimaryGeneratedColumn() id!: number;
  @Column({ unique: true }) handle!: string;
  @Column({ type: 'text', nullable: true }) bio!: string | null;
  @Column({ default: 'student' }) role!: string;
  @CreateDateColumn() createdAt!: Date;
  @UpdateDateColumn() updatedAt!: Date;
}
```

## The same Profile in Sequelize

Timestamps are added automatically. Note `allowNull: false` written explicitly on required fields.

```js
const Profile = sequelize.define('Profile', {
  handle: { type: DataTypes.STRING, allowNull: false, unique: true },
  bio: { type: DataTypes.TEXT, allowNull: true },
  role: { type: DataTypes.STRING, allowNull: false, defaultValue: 'student' },
});
```

## Money is not a float

Floating-point numbers cannot represent most decimal fractions exactly, so `0.1 + 0.2` is not `0.3` in JavaScript. Store prices as an integer number of paise, or use a decimal column (`Decimal` in Prisma, `type: 'decimal'` in TypeORM, `DataTypes.DECIMAL` in Sequelize). Drivers often return decimals as strings or special objects to avoid precision loss, so read the docs before doing arithmetic.

**Quiz:** In Sequelize, what happens if you omit allowNull on an attribute?

- [x] The column allows NULL, because allowNull defaults to true
- [ ] The column is NOT NULL
- [ ] Sequelize throws an error at define time
- [ ] The column becomes the primary key

*Answer:* The column allows NULL, because allowNull defaults to true. Sequelize's allowNull defaults to true. Prisma fields and TypeORM columns are required by default, so mixing the three can lead to accidental nullable columns.
