Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions src/content/docs/indexes-constraints.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,18 @@ A `PRIMARY KEY` constraint automatically has a `UNIQUE` constraint.
export const composite = pgTable('composite_example', {
id: integer('id'),
name: text('name'),
}, (t) => [{
unq: unique().on(t.id, t.name),
unq2: unique('custom_name').on(t.id, t.name)
}]);
}, (t) => [
unique().on(t.id, t.name),
unique('custom_name').on(t.id, t.name)
]);

// In Postgres 15.0+ NULLS NOT DISTINCT is available
// This example demonstrates both available usages
export const userNulls = pgTable('user_nulls_example', {
id: integer('id').unique("custom_name", { nulls: 'not distinct' }),
}, (t) => [{
unq: unique().on(t.id).nullsNotDistinct()
}]);
}, (t) => [
unique().on(t.id).nullsNotDistinct()
]);
```

```sql
Expand Down Expand Up @@ -411,9 +411,9 @@ If you define a `CHECK` constraint on a table it can limit the values in certain
username: text().notNull(),
age: integer(),
},
(table) => [{
checkConstraint: check("age_check1", sql`${table.age} > 21`),
}]
(table) => [
check("age_check1", sql`${table.age} > 21`),
]
);
```
```sql
Expand Down Expand Up @@ -629,10 +629,10 @@ Drizzle ORM provides a standalone `primaryKey` operator for that:
authorId: integer("author_id"),
bookId: integer("book_id"),
}, (table) => {
return [{
pk: primaryKey({ columns: [table.bookId, table.authorId] }),
pkWithCustomName: primaryKey({ name: 'custom_name', columns: [table.bookId, table.authorId] }),
}];
return [
primaryKey({ columns: [table.bookId, table.authorId] }),
primaryKey({ name: 'custom_name', columns: [table.bookId, table.authorId] }),
];
});
```

Expand Down Expand Up @@ -851,13 +851,13 @@ set return type for reference callback or use a standalone `foreignKey` operator
name: text("name"),
parentId: integer("parent_id"),
}, (table) => {
return [{
parentReference: foreignKey({
return [
foreignKey({
columns: [table.parentId],
foreignColumns: [table.id],
name: "custom_fk"
}),
}];
];
});
```

Expand Down Expand Up @@ -930,23 +930,23 @@ To declare multicolumn foreign keys you can use a dedicated `foreignKey` operato
firstName: text("firstName"),
lastName: text("lastName"),
}, (table) => {
return {
pk: primaryKey({ columns: [table.firstName, table.lastName]}),
};
return [
primaryKey({ columns: [table.firstName, table.lastName]}),
];
});

export const profile = pgTable("profile", {
id: serial("id").primaryKey(),
userFirstName: text("user_first_name"),
userLastName: text("user_last_name"),
}, (table) => {
return [{
userReference: foreignKey({
return [
foreignKey({
columns: [table.userFirstName, table.userLastName],
foreignColumns: [user.firstName, user.lastName]
name: "custom_fk"
})
}]
]
})
```

Expand Down Expand Up @@ -1029,10 +1029,10 @@ Drizzle ORM provides API for both `index` and `unique index` declaration:
name: text("name"),
email: text("email"),
}, (table) => {
return {
nameIdx: index("name_idx").on(table.name),
emailIdx: uniqueIndex("email_idx").on(table.email),
};
return [
index("name_idx").on(table.name),
uniqueIndex("email_idx").on(table.email),
];
});
```
```sql {5-6}
Expand Down