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
import { serial, text, integer, foreignKey, pgTable, AnyPgColumn } from "drizzle-orm/pg-core";
export const user = pgTable("user", {
id: serial("id"),
name: text("name"),
parentId: integer("parent_id").references((): AnyPgColumn => user.id)
});
// or
export const user = pgTable("user", {
id: serial("id"),
name: text("name"),
parentId: integer("parent_id"),
}, (table) => {
return {
parentReference: foreignKey({
columns: [table.parentId],
foreignColumns: [table.id],
name: "custom_fk"
}),
};
});
import { serial, text, integer, foreignKey, pgTable, AnyPgColumn } from "drizzle-orm/pg-core";
export const user = pgTable("user", {
id: serial("id"),
name: text("name"),
parentId: integer("parent_id").references((): AnyPgColumn => user.id)
});
// or
export const user = pgTable("user", {
id: serial("id"),
name: text("name"),
parentId: integer("parent_id"),
}, (table) => {
return {
parentReference: foreignKey({
columns: [table.parentId],
foreignColumns: [table.id],
name: "custom_fk"
}),
};
});
found this on Drizzle docs https://orm.drizzle.team/docs/indexes-constraints which solved my problem
2 replies
DTDrizzle Team
Created by JustKira on 1/19/2024 in #help
Drizzle ORM: Inferring Relation multiple Many to one relation
Schema
export const courses = mysqlTable("courses", {
id: varchar("id", { length: 128 })
.primaryKey()
.$defaultFn(() => `cou-${createId()}`),
name: varchar("name", { length: 128 }).unique().notNull(),
description: text("description"),
});

export const courseRelations = relations(courses, ({ many }) => ({
nodes: many(nodes),
dependentCourses: many(course_dependencies, {
relationName: "depended_courses",
}),
courseDependencies: many(course_dependencies, {
relationName: "course_dependencies",
}),
}));

export const course_dependencies = mysqlTable(
"course_dependencies",
{
id: varchar("id", { length: 128 })
.primaryKey()
.$defaultFn(() => `cou-dep-${createId()}`),
courseId: varchar("courseId", { length: 128 }).notNull(),
dependencyCourseId: varchar("dependencyCourseId", {
length: 128,
}).notNull(),
},
(t) => ({
unq: unique().on(t.dependencyCourseId, t.courseId),
})
);

export const course_dependenciesRelations = relations(
course_dependencies,
({ one }) => ({
course: one(courses, {
fields: [course_dependencies.courseId],
references: [courses.id],
}),
courseDependency: one(courses, {
fields: [course_dependencies.dependencyCourseId],
references: [courses.id],
}),
})
);
export const courses = mysqlTable("courses", {
id: varchar("id", { length: 128 })
.primaryKey()
.$defaultFn(() => `cou-${createId()}`),
name: varchar("name", { length: 128 }).unique().notNull(),
description: text("description"),
});

export const courseRelations = relations(courses, ({ many }) => ({
nodes: many(nodes),
dependentCourses: many(course_dependencies, {
relationName: "depended_courses",
}),
courseDependencies: many(course_dependencies, {
relationName: "course_dependencies",
}),
}));

export const course_dependencies = mysqlTable(
"course_dependencies",
{
id: varchar("id", { length: 128 })
.primaryKey()
.$defaultFn(() => `cou-dep-${createId()}`),
courseId: varchar("courseId", { length: 128 }).notNull(),
dependencyCourseId: varchar("dependencyCourseId", {
length: 128,
}).notNull(),
},
(t) => ({
unq: unique().on(t.dependencyCourseId, t.courseId),
})
);

export const course_dependenciesRelations = relations(
course_dependencies,
({ one }) => ({
course: one(courses, {
fields: [course_dependencies.courseId],
references: [courses.id],
}),
courseDependency: one(courses, {
fields: [course_dependencies.dependencyCourseId],
references: [courses.id],
}),
})
);
4 replies
DTDrizzle Team
Created by JustKira on 12/17/2023 in #help
update not working
nvm its problem with supabase RLS for some reason blocks api
2 replies