drizzle many-to-many

https://gist.github.com/barrybtw/f4c54bf7bdac98af34069363d7b1be87 Trying to setup a many-to-many relationship between calendar and user. But I'm getting some weird Typescript errors.
Argument of type 'Relations<"calendar_participant", { calendar: One<"calendar", true>; user: One<"user", true>; }>' is not assignable to parameter of type 'Table<TableConfig$3<Column<any, object, object>>>'.ts(2345)
Argument of type 'Relations<"calendar_participant", { calendar: One<"calendar", true>; user: One<"user", true>; }>' is not assignable to parameter of type 'Table<TableConfig$3<Column<any, object, object>>>'.
Type 'Relations<"calendar_participant", { calendar: One<"calendar", true>; user: One<"user", true>; }>' is missing the following properties from type 'Table<TableConfig$3<Column<any, object, object>>>': _, getSQL, [IsDrizzleTable]ts(2345)
Argument of type 'Relations<"calendar_participant", { calendar: One<"calendar", true>; user: One<"user", true>; }>' is not assignable to parameter of type 'Table<TableConfig$3<Column<any, object, object>>>'.ts(2345)
Argument of type 'Relations<"calendar_participant", { calendar: One<"calendar", true>; user: One<"user", true>; }>' is not assignable to parameter of type 'Table<TableConfig$3<Column<any, object, object>>>'.
Type 'Relations<"calendar_participant", { calendar: One<"calendar", true>; user: One<"user", true>; }>' is missing the following properties from type 'Table<TableConfig$3<Column<any, object, object>>>': _, getSQL, [IsDrizzleTable]ts(2345)
Gist
drizzle.schema.ts
GitHub Gist: instantly share code, notes, and snippets.
Solution:
oh wait im a dumb dumb i was using the relation definition instead of the table for the many relation input
Jump to solution
2 Replies
barry
barryOP17mo ago
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 errors are on both one-to-many relations between both user and calendar to the junction table
Solution
barry
barry17mo ago
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