Querying through junction tables? #837
-
I'm trying to query through a many-to-many junction table, but it seems like the TS typing fails while writing the code. For the following schema: // generateGuid is a canned expression that is used to create GUID ids on the server
// USERS
export const users = sqliteTable(
'users',
{
id: generateGuid('id').primaryKey(),
externalId: text('external_id').notNull(),
}
);
export const usersRelations = relations(users, ({ many }) => ({
userToOrgs: many(usersToOrganizations),
}));
//ORGS
export const organizations = sqliteTable(
'organizations',
{
id: generateGuid('id').primaryKey(),
externalId: text('external_id'),
}
);
export const organizationsRelations = relations(organizations, ({ many }) => ({
orgToUsers: many(usersToOrganizations),
}));
//JUNCTION
export const usersToOrganizations = sqliteTable(
'organization_user_junction',
{
userId: text('user_id')
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
organizationId: text('organization_id')
.notNull()
.references(() => organizations.id, { onDelete: 'cascade' }),
}
);
export const usersToOrganizationsRelations = relations(
usersToOrganizations,
({ one }) => ({
organization: one(organizations, {
fields: [usersToOrganizations.organizationId],
references: [organizations.id],
}),
user: one(users, {
fields: [usersToOrganizations.userId],
references: [users.id],
}),
})
); Auto-complete doesn't work for the const profile = await data.db.query.users.findFirst({
with: {
userToOrgs: {
where: // auto-complete doesn't work here at all
},
},
});
const organization = await db.query.organizations.findFirst({
with: {
orgToUsers: {
where: // auto-complete doesn't work here at all
},
},
}); One-To-Many relationships seem to work as expected, with auto-complete locating the correct types. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Solved it. I wasn't re-exporting the file that contained the Great work on this library! |
Beta Was this translation helpful? Give feedback.
Solved it.
I wasn't re-exporting the file that contained the
usersToOrganizations
table. Thus it wasn't getting picked up.Great work on this library!