DT
Drizzle Team•2mo ago
DRVGO

Error in relational queries

import { relations } from "drizzle-orm";
import { boolean, jsonb, pgTable, text, timestamp } from "drizzle-orm/pg-core";
import { generateId, generateTableName } from "../utils";

const genTableName = generateTableName();

// SCHEMAS

export const users = pgTable(genTableName("users"), {
id: text("id").primaryKey().notNull().unique(),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
username: text("username").notNull().unique(),
email: text("email").notNull().unique(),
avatarUrl: text("avatar_url"),
isVerified: boolean("is_verified").notNull().default(false),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});

export const profiles = pgTable(genTableName("profiles"), {
id: text("id").primaryKey().notNull().unique().$defaultFn(generateId),
userId: text("user_id")
.notNull()
.unique()
.references(() => users.id, {
onDelete: "cascade",
}),
phone: text("phone").unique(),
address: jsonb("address"),
isProfileCompleted: boolean("is_profile_completed").notNull().default(false),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});

// RELATIONS

export const usersRelations = relations(users, ({ one }) => ({
profile: one(profiles),
}));

export const profilesRelations = relations(profiles, ({ one }) => ({
user: one(users, {
fields: [profiles.userId],
references: [users.id],
}),
}));
import { relations } from "drizzle-orm";
import { boolean, jsonb, pgTable, text, timestamp } from "drizzle-orm/pg-core";
import { generateId, generateTableName } from "../utils";

const genTableName = generateTableName();

// SCHEMAS

export const users = pgTable(genTableName("users"), {
id: text("id").primaryKey().notNull().unique(),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
username: text("username").notNull().unique(),
email: text("email").notNull().unique(),
avatarUrl: text("avatar_url"),
isVerified: boolean("is_verified").notNull().default(false),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});

export const profiles = pgTable(genTableName("profiles"), {
id: text("id").primaryKey().notNull().unique().$defaultFn(generateId),
userId: text("user_id")
.notNull()
.unique()
.references(() => users.id, {
onDelete: "cascade",
}),
phone: text("phone").unique(),
address: jsonb("address"),
isProfileCompleted: boolean("is_profile_completed").notNull().default(false),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});

// RELATIONS

export const usersRelations = relations(users, ({ one }) => ({
profile: one(profiles),
}));

export const profilesRelations = relations(profiles, ({ one }) => ({
user: one(users, {
fields: [profiles.userId],
references: [users.id],
}),
}));
This is supposed to return the user with profile in the result. But, the intellisense is not working, and also the type in the result doesn't include the profile.
No description
8 Replies
rphlmr âš¡
rphlmr ⚡•2mo ago
👋 Could you share generateTableName and check that you included the relation in the drizzle client?
// schema should include exported relations too
const db = drizzle({ client, schema});
// schema should include exported relations too
const db = drizzle({ client, schema});
DRVGO
DRVGOOP•2mo ago
export function generateTableName(prefix?: string) {
return (name: string) => {
return prefix ? prefix + "__" + name : name;
};
}
export function generateTableName(prefix?: string) {
return (name: string) => {
return prefix ? prefix + "__" + name : name;
};
}
It's just a simple function Also
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

export const connection = postgres(process.env.DATABASE_URL!);
export const db = drizzle(connection, {
schema,
});
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

export const connection = postgres(process.env.DATABASE_URL!);
export const db = drizzle(connection, {
schema,
});
This is the db instance Hmm Weird Apparently the function is blocking the intellisense Removing the table name generator function works
rphlmr âš¡
rphlmr ⚡•2mo ago
can you use pgTableCreator from Drizzle?
rphlmr âš¡
rphlmr ⚡•2mo ago
Drizzle ORM - Goodies
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
DRVGO
DRVGOOP•2mo ago
I never tried it, wait let me check Damn! this is what I actually needed XD Thanks a lot!
rphlmr âš¡
rphlmr ⚡•2mo ago
Nice! 🫡
rphlmr âš¡
rphlmr ⚡•2mo ago
Discord is still a spyware :sweating:
No description
Want results from more Discord servers?
Add your server