JustKira
JustKira
DTDrizzle Team
Created by JustKira on 4/27/2024 in #help
Circular Reference Error when Using Self-Reference in Table Definition with Drizzle-ORM
I encountered an error while defining a PostgreSQL table using Drizzle-ORM. The issue arises when attempting to create a self-referencing array column that references the primary key of its own table. The error message suggests that there is an issue with type inference possibly due to the self-reference.
import { createId } from "@paralleldrive/cuid2";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
import {
pgTable,
varchar,
timestamp,
jsonb,
integer,
} from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";

const course = pgTable("course", {
id: varchar("id")
.primaryKey()
.$defaultFn(() => createId()),
name: varchar("name").unique().notNull(),
description: varchar("description").notNull(),
image_url: varchar("image_url"),
content_object: jsonb("content_object"),
version: integer("version").default(1).notNull(),
dependencies_courses: varchar("dependencies_courses").array().references(() => course.id),

created_at: timestamp("created_at", {
precision: 3,
})
.default(sql`CURRENT_TIMESTAMP(3)`)
.notNull(),
updated_at: timestamp("updated_at")
.default(sql`CURRENT_TIMESTAMP(3)`)
.$onUpdateFn(() => new Date())
.notNull(),
});

const insertCourseSchema = createInsertSchema(course);
interface NewCourse extends z.infer<typeof insertCourseSchema> {}

const selectCourseSchema = createSelectSchema(course);

interface Course extends z.infer<typeof selectCourseSchema> {}

export { course, insertCourseSchema, selectCourseSchema };

export type { NewCourse, Course };
import { createId } from "@paralleldrive/cuid2";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
import {
pgTable,
varchar,
timestamp,
jsonb,
integer,
} from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";

const course = pgTable("course", {
id: varchar("id")
.primaryKey()
.$defaultFn(() => createId()),
name: varchar("name").unique().notNull(),
description: varchar("description").notNull(),
image_url: varchar("image_url"),
content_object: jsonb("content_object"),
version: integer("version").default(1).notNull(),
dependencies_courses: varchar("dependencies_courses").array().references(() => course.id),

created_at: timestamp("created_at", {
precision: 3,
})
.default(sql`CURRENT_TIMESTAMP(3)`)
.notNull(),
updated_at: timestamp("updated_at")
.default(sql`CURRENT_TIMESTAMP(3)`)
.$onUpdateFn(() => new Date())
.notNull(),
});

const insertCourseSchema = createInsertSchema(course);
interface NewCourse extends z.infer<typeof insertCourseSchema> {}

const selectCourseSchema = createSelectSchema(course);

interface Course extends z.infer<typeof selectCourseSchema> {}

export { course, insertCourseSchema, selectCourseSchema };

export type { NewCourse, Course };
'course' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
'course' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
Expected Behavior: I expect to define a self-referencing column (dependencies_courses) that can store an array of course IDs, referencing the id column of the same course table without causing a type inference issue.
2 replies
DTDrizzle Team
Created by JustKira on 1/19/2024 in #help
Drizzle ORM: Inferring Relation multiple Many to one relation
Hi everyone, I'm working on a project using Drizzle ORM in a Next.js environment and I've run into a snag. My goal is to set up a database schema for an educational platform where courses have dependencies on other courses. Specifically, I'm trying to establish two types of relationships in my courses table: Course Dependencies: Identifying the prerequisite courses for a given course. Dependent Courses: Listing the courses that depend on a given course as a prerequisite. However, I'm encountering an error when trying to infer the relation for dependentCourses in my courses table. Here's the error message I received: Error
Error: There is not enough information to infer relation "__public__.courses.dependentCourses"
at normalizeRelation [...]
at Command.<anonymous> [...]

Node.js v21.4.0
Error: There is not enough information to infer relation "__public__.courses.dependentCourses"
at normalizeRelation [...]
at Command.<anonymous> [...]

Node.js v21.4.0
4 replies
DTDrizzle Team
Created by JustKira on 12/17/2023 in #help
update not working
export async function PATCH(request: NextRequest) {
const body = (await request.json()) as Partial<
Omit<Profile, "uuid" | "stripeId">
>;

const supabase = supabaseApiClient();
const { data, error } = await supabase.auth.getUser();

if (error) {
return NextResponse.json(error, { status: error.status });
}

const profileUpdate = await db.transaction(async (tx) => {
await setTxAuthUUID(tx, data.user.id);
await setTxAuthedRole(tx);

try {
await tx
.update(profiles)
.set({ ...body })
.where(eq(profiles.uuid, data.user.id));

return true;
} catch (error) {
return false;
}
});

if (profileUpdate) {
return NextResponse.json({}, { status: 201 });
} else {
return NextResponse.json({}, { status: 400 });
}
}
export async function PATCH(request: NextRequest) {
const body = (await request.json()) as Partial<
Omit<Profile, "uuid" | "stripeId">
>;

const supabase = supabaseApiClient();
const { data, error } = await supabase.auth.getUser();

if (error) {
return NextResponse.json(error, { status: error.status });
}

const profileUpdate = await db.transaction(async (tx) => {
await setTxAuthUUID(tx, data.user.id);
await setTxAuthedRole(tx);

try {
await tx
.update(profiles)
.set({ ...body })
.where(eq(profiles.uuid, data.user.id));

return true;
} catch (error) {
return false;
}
});

if (profileUpdate) {
return NextResponse.json({}, { status: 201 });
} else {
return NextResponse.json({}, { status: 400 });
}
}
this is my api endpoint for patching i have test ...body if values are correct and if data.user.id exist everything should be fine and return true but when i check database it doesnt seem to be updated for more context im using NEXTJS + react query
2 replies