We will keep the tables and sample data the same, then change the code that loads related rows. This covers one relational query, rather than the full application upgrade. For package, configuration, and migration changes, use the full upgrade guide (Drizzle docs, opens in a new tab).

Run both versions

The playground has Drizzle 0.45.4 on the left and rc.5 on the right. Each side runs against its own fresh PostgreSQL database in the browser, using the same seed data.

Load the playground, then press Run both. You can switch between schema.ts, query.ts, and the shared seed.ts to compare the code and data.

Open the full playground (drizzle.run, opens in a new tab) to edit the examples. The embed is read only.

Start with the same tables

Our example has two users, Ada and Linus, and three posts:

Post ID Title Author
1 Getting started with Drizzle Ada
2 Loading related data Ada
3 A second author Linus

posts.authorId stores the ID of the user who wrote the post. These table definitions work in both versions:

schema.ts
import { pgTable, integer, text } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: integer('id').primaryKey(),
name: text('name').notNull(),
});
export const posts = pgTable('posts', {
id: integer('id').primaryKey(),
title: text('title').notNull(),
authorId: integer('author_id').notNull().references(() => users.id),
});

The foreign key created by .references() checks that the author exists in the database. The relation we define next tells db.query how to load that author. Defining a query relation does not create or change a database constraint.

Define the author relation

In 0.45.4, relations() defines relationships for one table at a time. In rc.5, defineRelations() receives the tables and lets you define their relationships together.

Diff layout

Before · 0.45.4

After · 1.0.0-rc.5

relations.ts
// drizzle-orm@0.45.4
import { relations } from 'drizzle-orm';
import { users, posts } from './schema';
export const postsRelations = relations(posts, ({ one }) => ({
author: one(users, {
fields: [posts.authorId],
references: [users.id],
}),
}));
relations.ts
// drizzle-orm@1.0.0-rc.5
import { defineRelations } from 'drizzle-orm';
import { users, posts } from './schema';
export const relations = defineRelations({ users, posts }, (r) => ({
posts: {
author: r.one.users({
from: r.posts.authorId,
to: r.users.id,
}),
},
}));

from is the starting column, posts.authorId. to is the matching column, users.id. We named the relation author, so that is the property we will request in with and read in the result.

one describes what this query loads: a single related user. It does not mean that only one post can belong to that user. Posts 1 and 2 both belong to Ada.

In the article, tables and relations are separate files. The playground keeps both in schema.ts so you can compare each version in one tab.

Pass the definitions to your client

These application examples use the node-postgres driver and read DATABASE_URL from your server environment. They assume your database already contains the tables and sample rows above.

In 0.45.4, the schema option contains the tables and their relation definitions. In rc.5, pass the result of defineRelations() through relations.

Diff layout

Before · 0.45.4

After · 1.0.0-rc.5

db.ts
// drizzle-orm@0.45.4
import { Pool } from 'pg';
import { drizzle } from 'drizzle-orm/node-postgres';
import { users, posts } from './schema';
import { postsRelations } from './relations';
const client = new Pool({ connectionString: process.env.DATABASE_URL });
export const db = drizzle(client, {
schema: { users, posts, postsRelations },
});
db.ts
// drizzle-orm@1.0.0-rc.5
import { Pool } from 'pg';
import { drizzle } from 'drizzle-orm/node-postgres';
import { relations } from './relations';
const client = new Pool({ connectionString: process.env.DATABASE_URL });
export const db = drizzle({ client, relations });

For an existing node-postgres client, rc.5 takes client inside the configuration object. Your table definitions still belong in the project. Removing the old initialization option does not mean deleting schema.ts.

Drizzle Run supplies its own connected db through @drizzle-run/runtime/context and binds the exported definitions for each version. That is why its query.ts starts with a runtime import instead of the application setup above.

Load post 1 with its author

The equality filter changes from a callback to an object. For this query, with: { author: true } stays the same:

Diff layout

Before · 0.45.4

After · 1.0.0-rc.5

query.ts
// drizzle-orm@0.45.4
import { db } from './db';
export const result = await db.query.posts.findMany({
where: (posts, { eq }) => eq(posts.id, 1),
with: { author: true },
});
query.ts
// drizzle-orm@1.0.0-rc.5
import { db } from './db';
export const result = await db.query.posts.findMany({
where: { id: 1 },
with: { author: true },
});

Both versions return:

result
[
{
"id": 1,
"title": "Getting started with Drizzle",
"authorId": 1,
"author": {
"id": 1,
"name": "Ada"
}
}
]

findMany() returns a list, even when only one post matches. author is an object because we defined it with one.

In the full playground, change the ID on both sides to 2, 3, or 99. Post 2 also has Ada as its author. Post 3 belongs to Linus. No post has ID 99, so both queries return an empty list.

Check optional filters when upgrading

Since rc.5, explicitly passing undefined in a relational filter throws. Use EmptyFilter from drizzle-orm/relations when you intentionally want to skip a condition. You can also omit the field entirely.

Here, findPosts(postId?: number) accepts an optional post ID. Calling findPosts(1) loads post 1 with its author. Calling findPosts() deliberately leaves the query unfiltered:

Diff layout

Before · 0.45.4

After · 1.0.0-rc.5

optional-query.ts
// drizzle-orm@0.45.4
import { db } from './db';
async function findPosts(postId?: number) {
return db.query.posts.findMany({
where: (posts, { eq }) =>
postId === undefined ? undefined : eq(posts.id, postId),
with: { author: true },
});
}
export const result = await findPosts();
optional-query.ts
// drizzle-orm@1.0.0-rc.5
import { EmptyFilter } from 'drizzle-orm/relations';
import { db } from './db';
async function findPosts(postId?: number) {
return db.query.posts.findMany({
where: { id: postId ?? EmptyFilter },
with: { author: true },
});
}
export const result = await findPosts();

In rc.5, postId ?? EmptyFilter keeps the ID when it is provided and skips the condition when it is absent. Skipping the only condition returns all three posts, each with its author. Use this only when returning every row is the intended behavior. If the ID is required, validate it before querying instead.

The playground includes this helper below the first query. Its second result card shows all three posts. Change findPosts() to findPosts(1) on both sides to compare a supplied ID.

This concerns db.query filters. Do not apply it as a blanket rule to conditional filters in the SQL-style query builder.

The relations migration guide (Drizzle docs, opens in a new tab) covers other query shapes and upgrade details. For more examples of what relational queries v2 can do, including many-to-many relationships, see Relational Queries v2.