Back to Drizzle Orm

Sequences

src/content/docs/sequences.mdx

latest1.7 KB
Original Source

import IsSupportedChipGroup from '@mdx/IsSupportedChipGroup.astro'; import Callout from '@mdx/Callout.astro'; import Tab from '@mdx/Tab.astro'; import Tabs from '@mdx/Tabs.astro';

Sequences

<IsSupportedChipGroup chips={{ 'PostgreSQL': true, 'SQLite': false, 'MySQL': false, 'SingleStore': false, 'MSSQL': false, 'CockroachDB': true }} />

Sequences in PostgreSQL and CockroachDB are special single-row tables created to generate unique identifiers, often used for auto-incrementing primary key values. They provide a thread-safe way to generate unique sequential values across multiple sessions.

<Tabs items={['PostgreSQL', 'CockroachDB']}> <Tab>

ts
import { pgSchema, pgSequence } from "drizzle-orm/pg-core";

// No params specified
export const customSequence = pgSequence("name");

// Sequence with params
export const customSequence = pgSequence("name", {
      startWith: 100,
      maxValue: 10000,
      minValue: 100,
      cycle: true,
      cache: 10,
      increment: 2
});

// Sequence in custom schema
export const customSchema = pgSchema('custom_schema');
export const customSequence = customSchema.sequence("name");
</Tab> <Tab> ```ts import { cockroachSchema, cockroachSequence } from "drizzle-orm/cockroach-core";

// No params specified export const customSequence = cockroachSequence("name");

// Sequence with params export const customSequence = cockroachSequence("name", { startWith: 100, maxValue: 10000, minValue: 100, cycle: true, cache: 10, increment: 2 });

// Sequence in custom schema export const customSchema = cockroachSchema('custom_schema'); export const customSequence = customSchema.sequence("name");

</Tab>
</Tabs>