The sole purpose of Drizzle relations is to let you query your relational data in the most simple and consise way:
Relational queries
Select with joins
One-to-one
Drizzle ORM provides you an API to define one-to-one relations between tables with the relations operator.
An example of a one-to-one relation between users and users, where a user can invite another (this example uses a self reference):
Another example would be a user having a profile information stored in separate table. In this case, because the foreign key is stored in the “profile_info” table, the user relation have neither fields or references. This tells Typescript that user.profileInfo is nullable:
One-to-many
Drizzle ORM provides you an API to define one-to-many relations between tables with relations operator.
Example of one-to-many relation between users and posts they’ve written:
Now lets add comments to the posts:
Many-to-many
Drizzle ORM provides you an API to define many-to-many relations between tables through so called junction or join tables,
they have to be explicitly defined and store associations between related tables.
Example of many-to-many relation between users and groups:
Foreign keys
You might’ve noticed that relations look similar to foreign keys — they even have a references property. So what’s the difference?
While foreign keys serve a similar purpose, defining relations between tables, they work on a different level compared to relations.
Foreign keys are a database level constraint, they are checked on every insert/update/delete operation and throw an error if a constraint is violated.
On the other hand, relations are a higher level abstraction, they are used to define relations between tables on the application level only.
They do not affect the database schema in any way and do not create foreign keys implicitly.
What this means is relations and foreign keys can be used together, but they are not dependent on each other.
You can define relations without using foreign keys (and vice versa), which allows them to be used with databases that do not support foreign keys.
The following two examples will work exactly the same in terms of querying the data using Drizzle relational queries.
You can specify actions that should occur when the referenced data in the parent table is modified. These actions are known as “foreign key actions.” PostgreSQL provides several options for these actions.
On Delete/ Update Actions
CASCADE: When a row in the parent table is deleted, all corresponding rows in the child table will also be deleted. This ensures that no orphaned rows exist in the child table.
NO ACTION: This is the default action. It prevents the deletion of a row in the parent table if there are related rows in the child table. The DELETE operation in the parent table will fail.
RESTRICT: Similar to NO ACTION, it prevents the deletion of a parent row if there are dependent rows in the child table. It is essentially the same as NO ACTION and included for compatibility reasons.
SET DEFAULT: If a row in the parent table is deleted, the foreign key column in the child table will be set to its default value if it has one. If it doesn’t have a default value, the DELETE operation will fail.
SET NULL: When a row in the parent table is deleted, the foreign key column in the child table will be set to NULL. This action assumes that the foreign key column in the child table allows NULL values.
Analogous to ON DELETE there is also ON UPDATE which is invoked when a referenced column is changed (updated). The possible actions are the same, except that column lists cannot be specified for SET NULL and SET DEFAULT. In this case, CASCADE means that the updated values of the referenced column(s) should be copied into the referencing row(s).
in drizzle you can add foreign key action using references() second argument.
type of the actions
In the following example, adding onDelete: 'cascade' to the author field on the posts schema means that deleting the user will also delete all related Post records.
For constraints specified with the foreignKey operator, foreign key actions are defined with the syntax:
Disambiguating relations
Drizzle also provides the relationName option as a way to disambiguate
relations when you define multiple of them between the same two tables. For
example, if you define a posts table that has the author and reviewer
relations.