Many-to-one relation
A post belongs to one author and the query result should expose one author object.
Drizzle ORM · Function
r.one.<table> defines a relation that returns one related object instead of an array. Configure its source and target columns, and optionally control requiredness, aliases, and conditional matching.
Use r.one.<table> inside defineRelations when a relation should resolve to one related record instead of an array. Specify the source and target columns, then use options such as optional, alias, or where to describe requiredness, naming, or conditional matching.
r.one.<table>({ from, to, optional?, alias?, where? })| Name | Type | Description |
|---|---|---|
from | relation column | Source table and column for the relation. |
to | relation column | Target table and column for the relation. |
optional | boolean | Whether the related property is optional in the inferred type. |
alias | string | Custom relation name used to distinguish relations. |
where | object | Condition used to select matching related records. |
A relation definition in which the named property resolves to one related object rather than an array.
r.one.<table> to model a relation whose result is one related object instead of a collection.from option names the source table and column where the relation begins.optional to false changes the inferred relation property to required.alias to distinguish relations that would otherwise have the same target.where option supports conditional relation matching, including polymorphic relationships.optional: false only when the related entity is guaranteed to exist; it makes the relation property required in the inferred type.where condition changes which related record is selected and is useful for polymorphic relations.const relations = defineRelations({ users, posts }, (r) => ({
posts: {
author: r.one.users({
from: r.posts.ownerId,
to: r.users.id,
optional: false,
}),
},
}));Defines a required one-to-one-style relation from each post to its author.
A post belongs to one author and the query result should expose one author object.
Several relations point to the same target table and need distinct names.
A relation should match only records satisfying a discriminator or status condition.
r.one.users represent?r.one.users declares that the relation returns one user object rather than an array. The from field identifies the source column, while to identifies the referenced column.
Follow-up: How can the relation be constrained to a subset of users? Add a where condition, such as a verified flag, to filter the related records.
optional: false?optional: false makes the relation property required at the type level. Use it only when the related entity is guaranteed to exist.
Follow-up: What should be changed when the related entity may be absent? Omit the non-optional setting or configure the relation as optional so the type can represent absence.
alias?alias provides a custom name for the relation when defining relations. This helps distinguish relations when multiple relations target the same table.
Follow-up: How can relation selection be made conditional? Supply a where object to define the matching condition for the relation.
defineRelationsr.manyrelational queriesaliaswhere