Skip to content

[BUG]: view with join in the query builder generate invalid SQL #5112

@kyscott18

Description

@kyscott18

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.44.0

What version of drizzle-kit are you using?

n/a

Other packages

No response

Describe the Bug

This query is unable to find "caretakerName" and instead generates an SQL query with "undefined".

import { drizzle } from "drizzle-orm/node-postgres";
import { pgTable, pgView } from "drizzle-orm/pg-core";
import { eq } from "drizzle-orm";

const animal = pgTable("animal", (t) => ({
    id: t.text().primaryKey(),
    name: t.text().notNull(),
    caretakerId: t.text().notNull(),
}));
const caretaker = pgTable("caretaker", (t) => ({
    id: t.text().primaryKey(),
    caretakerName: t.text().notNull(),
}));
const animalWithCaretakerView = pgView("animal_with_caretaker_view").as(
    (qb) =>
        qb
        .select({
            id: animal.id,
            animalName: animal.name,
            caretakerName: caretaker.caretakerName,
        })
        .from(animal)
        .innerJoin(caretaker, eq(animal.caretakerId, caretaker.id)),
);

const schema = { animal, caretaker, animalWithCaretakerView };
const db = drizzle("", {schema})

console.log(db.select().from(animalWithCaretakerView).toSQL().sql)
// select "id", "name", "undefined" from "animal_with_caretaker_view"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions