Select with relation

Is there a way to get relations when using db.select().from(table)I can't get the db.query.table.findManyto work, so if someone could help me get that setup that would be fine too
10 Replies
Andrii Sherman
I can help you with setting up relational quries, what didn't work for you?
Eko
EkoOP2y ago
Basically whenever I try following the docs I get an error like postgreserror: unrecognized configuration parameter "schema" This is what I had setup
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from 'postgres'
import * as schema from "@/db/migrations/schema"

const connectionString: string | undefined = process.env.DATABASE_URL

const client = postgres(connectionString!, { max: 1 })
const db = drizzle(client, {schema});
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from 'postgres'
import * as schema from "@/db/migrations/schema"

const connectionString: string | undefined = process.env.DATABASE_URL

const client = postgres(connectionString!, { max: 1 })
const db = drizzle(client, {schema});
And my schema for question looks like this
export const question = pgTable(
"question",
{
id: serial("id").notNull(),
title: varchar("title", { length: 100 }).notNull(),
desc: varchar("desc", { length: 100 }),
createdAt: timestamp("createdAt", {
precision: 6,
mode: "string",
}).defaultNow(),
option1: varchar("option1", { length: 30 }).notNull(),
option2: varchar("option2", { length: 30 }).notNull(),
ownerId: text("ownerId").notNull(),
image1: text("image1"),
image2: text("image2"),
isDeleted: boolean("isDeleted").notNull(),
},
(table) => {
return {
questionPkey: primaryKey(table.id, table.id),
};
}
);

export const questionRelations = relations(question, ({ many })=>({
votes: many(questionVotes),
likes: many(questionLikes),
}))
export const question = pgTable(
"question",
{
id: serial("id").notNull(),
title: varchar("title", { length: 100 }).notNull(),
desc: varchar("desc", { length: 100 }),
createdAt: timestamp("createdAt", {
precision: 6,
mode: "string",
}).defaultNow(),
option1: varchar("option1", { length: 30 }).notNull(),
option2: varchar("option2", { length: 30 }).notNull(),
ownerId: text("ownerId").notNull(),
image1: text("image1"),
image2: text("image2"),
isDeleted: boolean("isDeleted").notNull(),
},
(table) => {
return {
questionPkey: primaryKey(table.id, table.id),
};
}
);

export const questionRelations = relations(question, ({ many })=>({
votes: many(questionVotes),
likes: many(questionLikes),
}))
Andrii Sherman
What version of drizzle orm are you using? 0.26.0?
Eko
EkoOP2y ago
yes is it possible that I did not properly push my schemas?
Andrii Sherman
do you have anything except of table and relations object in your schema file?
Eko
EkoOP2y ago
I got multiple tables but nothing else
Andrii Sherman
oh, it's a bit tough, can't repro locally will dig into it more
Eko
EkoOP2y ago
I might have fixed it... Ill update in a bit @Andrew Sherman I seem to have had quite a few issues with an outdated config for drizzle now it's been cleared up but one question tho, what is the correct order of drizzle-kit commands and how can I make it automatically push schemas to the db?
Motomo
Motomo16mo ago
Hey there, I just setup drizzle and got the same error can you point me to a direction on how you fixed it ? I even tried with a schema of a single table and got the same issue trying to run
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";
import 'dotenv/config'
import path from "path";
import { readdirSync } from "fs";

const connectionString = process.env.DATABASE_URL as string;
const sql = postgres(connectionString, { max: 1 });
const db = drizzle(sql);

async function run() {
const migrationsFolder = path.join(__dirname, '..', 'drizzle');
console.log(readdirSync(migrationsFolder))
await migrate(db, { migrationsFolder });
process.exit(0);
}

run();
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";
import 'dotenv/config'
import path from "path";
import { readdirSync } from "fs";

const connectionString = process.env.DATABASE_URL as string;
const sql = postgres(connectionString, { max: 1 });
const db = drizzle(sql);

async function run() {
const migrationsFolder = path.join(__dirname, '..', 'drizzle');
console.log(readdirSync(migrationsFolder))
await migrate(db, { migrationsFolder });
process.exit(0);
}

run();
So I pushed the schema (this worked correclty) but when trying to run a query in the code got the same issue
@Injectable()
export class PostgresService implements OnModuleInit {
db: PostgresJsDatabase<typeof schema>;
constructor(private readonly config: ConfigService) { }

onModuleInit() {
console.log(this.config.databaseUrl())
const client = postgres(this.config.databaseUrl());
this.db = drizzle(client, { schema });
}
}
@Injectable()
export class PostgresService implements OnModuleInit {
db: PostgresJsDatabase<typeof schema>;
constructor(private readonly config: ConfigService) { }

onModuleInit() {
console.log(this.config.databaseUrl())
const client = postgres(this.config.databaseUrl());
this.db = drizzle(client, { schema });
}
}
(I checked that everything was defined correcly (schema path, db url)) Ok stypid mistake on my part I just reused the url of the database from Prisma and they use a ?schema=public instead of ?search_path=public in the Url
West side ⁉
West side ⁉15mo ago
so you're saying drizzle uses ?search_path=public? YES THATS WHAT YOU MEAN THANK YOU DUDE HOLY, I SPENT FUCKING HOURS TRYING TO FIX
Want results from more Discord servers?
Add your server