ยฉ 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Teamโ€ข3y agoโ€ข
1 reply
NotWhoYouThink

Relational query `extras` are not properly adding sql

When I try to use the drizzle orm relational query like it is show in the docs I run into an error. Rather than taking the sql that I say and then adding as to alias the value it just requests the alias.

const data = await db.query.user.findMany({
    columns: {
        id: true
    },
    extras(user, { sql }) {
        return {
            loweredName: sql`lower(${user.firstName})`.as(
                "lowered_name"
            )
        };
    }
});
const data = await db.query.user.findMany({
    columns: {
        id: true
    },
    extras(user, { sql }) {
        return {
            loweredName: sql`lower(${user.firstName})`.as(
                "lowered_name"
            )
        };
    }
});

or
const data = await db.query.user.findMany({
    columns: {
        id: true
    },
    extras: {
        loweredName: sql`lower(${user.firstName})`.as("lowered_name")
    }
});
const data = await db.query.user.findMany({
    columns: {
        id: true
    },
    extras: {
        loweredName: sql`lower(${user.firstName})`.as("lowered_name")
    }
});


Both of these make the following request:
select "id", "lowered_name" from "user"
select "id", "lowered_name" from "user"

Rather than the expected:
select lower("user"."firstName") as "lowered_name" from "user"
select lower("user"."firstName") as "lowered_name" from "user"


Also as a side note I am unable to use a string to function record for the extras like in the example.
await db.query.users.findMany({
    extras: {
        loweredName: (users, { sql }) => sql`lower(${users.name})`.as('lowered_name'),
    },
})
await db.query.users.findMany({
    extras: {
        loweredName: (users, { sql }) => sql`lower(${users.name})`.as('lowered_name'),
    },
})

^This throws an error. It says it expects a type of Alias<unkown>^
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Bug: Count in Extras (Relational)
Drizzle TeamDTDrizzle Team / help
9mo ago
Simple relational query in sqlite
Drizzle TeamDTDrizzle Team / help
3y ago
Optional/Default value for Relational `Extras`
Drizzle TeamDTDrizzle Team / help
3y ago
Nullable relational query?
Drizzle TeamDTDrizzle Team / help
3y ago