> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/kysely-org/kysely/llms.txt
> Use this file to discover all available pages before exploring further.

# Generated & GeneratedAlways

> Shortcuts for defining database-generated columns in Kysely

## Type Signatures

```typescript theme={null}
type Generated<S> = ColumnType<S, S | undefined, S>

type GeneratedAlways<S> = ColumnType<S, never, never>
```

## Overview

These utility types are shortcuts for common patterns when working with database-generated columns:

* **`Generated<T>`** - For columns that are optional in inserts and updates (e.g., auto-increment IDs, default values)
* **`GeneratedAlways<T>`** - For columns that cannot be inserted or updated (e.g., `GENERATED ALWAYS AS IDENTITY` columns)

## Generated

The `Generated<T>` type is useful for columns that the database can generate automatically, but can also be provided manually.

### Type Parameters

<ParamField path="S" type="type" required>
  The TypeScript type for the column across all operations.
</ParamField>

### Behavior

* **Select**: Returns type `S`
* **Insert**: Accepts `S | undefined` (optional)
* **Update**: Accepts `S` (optional, as all update fields are optional)

### Examples

#### Auto-Increment ID

```typescript theme={null}
interface PersonTable {
  id: Generated<number>
  name: string
}

// Insert without ID (database generates it)
await db.insertInto('person')
  .values({ name: 'John' })
  .execute()

// Insert with explicit ID
await db.insertInto('person')
  .values({ id: 123, name: 'Jane' })
  .execute()

// Select returns number
const person = await db
  .selectFrom('person')
  .selectAll()
  .executeTakeFirst()
// person.id is number
```

#### Timestamp with Default Value

```typescript theme={null}
interface PostTable {
  id: Generated<number>
  title: string
  created_at: Generated<Date>
}

// created_at is optional in insert
await db.insertInto('post')
  .values({ title: 'Hello World' })
  .execute()
```

## GeneratedAlways

The `GeneratedAlways<T>` type is for columns that are always generated by the database and cannot be manually set.

### Type Parameters

<ParamField path="S" type="type" required>
  The TypeScript type returned when selecting this column.
</ParamField>

### Behavior

* **Select**: Returns type `S`
* **Insert**: Not allowed (`never`)
* **Update**: Not allowed (`never`)

### Examples

#### PostgreSQL GENERATED ALWAYS AS IDENTITY

```postgresql theme={null}
CREATE TABLE person (
  id INTEGER GENERATED ALWAYS AS IDENTITY,
  name TEXT NOT NULL
);
```

```typescript theme={null}
interface PersonTable {
  id: GeneratedAlways<number>
  name: string
}

// Insert - cannot provide id
await db.insertInto('person')
  .values({ name: 'John' })
  .execute()

await db.insertInto('person')
  .values({ id: 123, name: 'Jane' }) // Type error!
  .execute()

// Update - cannot update id
await db.updateTable('person')
  .set({ id: 456 }) // Type error!
  .where('name', '=', 'John')
  .execute()
```

#### Computed Columns

```postgresql theme={null}
CREATE TABLE product (
  price DECIMAL(10, 2),
  tax_rate DECIMAL(4, 2),
  total_price DECIMAL(10, 2) GENERATED ALWAYS AS (price * (1 + tax_rate)) STORED
);
```

```typescript theme={null}
interface ProductTable {
  price: number
  tax_rate: number
  total_price: GeneratedAlways<number>
}

// total_price is read-only
await db.insertInto('product')
  .values({
    price: 100,
    tax_rate: 0.2
  })
  .execute()

const product = await db
  .selectFrom('product')
  .selectAll()
  .executeTakeFirst()
// product.total_price is 120
```

## Comparison

| Type                 | Select | Insert           | Update  | Use Case                           |
| -------------------- | ------ | ---------------- | ------- | ---------------------------------- |
| `Generated<T>`       | `T`    | `T \| undefined` | `T`     | Auto-increment IDs, default values |
| `GeneratedAlways<T>` | `T`    | `never`          | `never` | Identity columns, computed columns |

## Related Types

* [`ColumnType<SelectType, InsertType, UpdateType>`](/api/types/column-type) - The underlying generic type
* [`Selectable<T>`](/api/types/selectable) - Extract select types from a table interface
* [`Insertable<T>`](/api/types/insertable) - Extract insert types from a table interface
* [`Updateable<T>`](/api/types/updateable) - Extract update types from a table interface

## Source

[View source on GitHub](https://github.com/kysely-org/kysely/blob/master/src/util/column-type.ts)
