How should I set this up?

Gist
drizzle.schema.ts
GitHub Gist: instantly share code, notes, and snippets.
1 Reply
barry
barryOP16mo ago
So the idea is one user can create many calendars. One calendar can have one author. One calendar can also have many participants But i'm not sure how to structure that last relation Lemm walk through it, might make sense
export const user = pgTable(
'user',
{
id: serial('id').primaryKey(),
username: varchar('username', { length: 255 }).unique(),
password_hash: varchar('password_hash', { length: 255 }).notNull(),
password_salt: varchar('password_salt', { length: 255 }).notNull(),
name: varchar('name', { length: 255 }),
},
(table) => {
return {
usernameIdx: uniqueIndex('username_idx').on(table.username),
idIdx: uniqueIndex('id_idx').on(table.id),
};
},
);
export const user = pgTable(
'user',
{
id: serial('id').primaryKey(),
username: varchar('username', { length: 255 }).unique(),
password_hash: varchar('password_hash', { length: 255 }).notNull(),
password_salt: varchar('password_salt', { length: 255 }).notNull(),
name: varchar('name', { length: 255 }),
},
(table) => {
return {
usernameIdx: uniqueIndex('username_idx').on(table.username),
idIdx: uniqueIndex('id_idx').on(table.id),
};
},
);
We've got a user storing all this, which has a one to many relationship with both the session table and calendar table like this
export const user_relations = relations(user, ({ many }) => ({
session: many(session),
calendar: many(calendar_to_user_relations),
}));
export const user_relations = relations(user, ({ many }) => ({
session: many(session),
calendar: many(calendar_to_user_relations),
}));
calendar_to_user_relations is a junction/join table Session is irrelevant to the error so let's skip to the calendar table
export const calendar = pgTable(
'calendar',
{
id: serial('id').primaryKey(),
name: varchar('name', { length: 255 }).notNull(),
description: varchar('description', { length: 255 }),
time_of_creation: timestamp('creation_date').defaultNow(),
author_id: serial('author_id').notNull(),
},
(table) => {
return {
nameIdx: uniqueIndex('name_idx').on(table.name),
};
},
);
export const calendar = pgTable(
'calendar',
{
id: serial('id').primaryKey(),
name: varchar('name', { length: 255 }).notNull(),
description: varchar('description', { length: 255 }),
time_of_creation: timestamp('creation_date').defaultNow(),
author_id: serial('author_id').notNull(),
},
(table) => {
return {
nameIdx: uniqueIndex('name_idx').on(table.name),
};
},
);
And it's one to many relationship with the junction table
export const calendar_relations = relations(calendar, ({ many }) => ({
calendar_to_user_relations: many(calendar_to_user_relations),
}));
export const calendar_relations = relations(calendar, ({ many }) => ({
calendar_to_user_relations: many(calendar_to_user_relations),
}));
The junction table looks like this
export const calendar_participant = pgTable('calendar_participant', {
id: serial('id').primaryKey(),
calendar_id: serial('calendar_id').notNull(),
user_id: serial('user_id').notNull(),
});

export const calendar_to_user_relations = relations(
calendar_participant,
({ one }) => ({
calendar: one(calendar, {
fields: [calendar_participant.calendar_id],
references: [calendar.id],
}),
user: one(user, {
fields: [calendar_participant.user_id],
references: [user.id],
}),
}),
);
export const calendar_participant = pgTable('calendar_participant', {
id: serial('id').primaryKey(),
calendar_id: serial('calendar_id').notNull(),
user_id: serial('user_id').notNull(),
});

export const calendar_to_user_relations = relations(
calendar_participant,
({ one }) => ({
calendar: one(calendar, {
fields: [calendar_participant.calendar_id],
references: [calendar.id],
}),
user: one(user, {
fields: [calendar_participant.user_id],
references: [user.id],
}),
}),
);
The error is on the one to many relations between both user and calendar with the junction table. oh wait im a dumb dumb i was using the relation definition instead of the table for the many relation input
Want results from more Discord servers?
Add your server