---
title: 使用原生 BigInt 主键（MySQL 与 PostgreSQL）
---

来源：https://mikro-orm.io/docs/using-bigint-pks

从 v6 开始，`bigint` 由原生的 `BigInt` 类型表示，因此不需要在装饰器选项中显式指定类型：

```ts
@PrimaryKey()
id: bigint;
```

你也可以指定希望把 bigint 映射到的目标类型：

```ts
@PrimaryKey({ type: new BigIntType('bigint') })
id1: bigint;

@PrimaryKey({ type: new BigIntType('string') })
id2: string;

@PrimaryKey({ type: new BigIntType('number') })
id3: number;
```

> 映射到 `number` 类型时，JavaScript 无法表示 `bigint` 的所有可能取值——只有不超过 `Number.MAX_SAFE_INTEGER`（2^53 - 1）的值才是安全的。
