Relations module - or condition

Hi there! I have a teams table, an users table, and a team_members table that connects these two. A team also has an ownerId column. Currently I have a query that fetches all the teams of an userId whether they are connected through the ownerId column or the join table. I was wondering if this is possible using the new relations module? Since this is a many-to-many, but also an one-to-many, with an 'or' condition.
19 Replies
smoke
smoke14mo ago
Anyone that could help me out with this? Especially because this is some ‘or’ condition, not quite sure I expect something like this, similar to the some property from Prisma, but this currently does not exist in the relational queries:
const userId = "foo";

await db.query.teams.findMany({
where: (team, { eq, or }) => or(
eq(team.ownerId, userId),
in(team.teamMembers, userId)
),
});
const userId = "foo";

await db.query.teams.findMany({
where: (team, { eq, or }) => or(
eq(team.ownerId, userId),
in(team.teamMembers, userId)
),
});
Andrii Sherman
Andrii Sherman14mo ago
I will add all conditions to callback, just missed this before release But you can always import or from drizzle-orm Should work fine
Andrii Sherman
Andrii Sherman14mo ago
You can see both usages here
smoke
smoke14mo ago
I understand! But is that the correct way of checking if that specific userId is connected through the join table? I just thought of a similar method as the some property from Prisma, not too sure though Because I can’t access teamMembers under the team property, like a deep condition. Anyone that could help with this? I’d really appreciate it! For example, this is how I currently get all teams from an user whether they are the owner or connected through the join table:
const userId = "foo";

.leftJoin(teamMembers, eq(team.id, teamMembers.teamId))
.where(or(eq(team.ownerId, userId), eq(teamMembers.userId, userId)))
const userId = "foo";

.leftJoin(teamMembers, eq(team.id, teamMembers.teamId))
.where(or(eq(team.ownerId, userId), eq(teamMembers.userId, userId)))
But I would like to use the new relations module for this.
Andrii Sherman
Andrii Sherman14mo ago
Was a bit busy Will take a look now
smoke
smoke14mo ago
Of course no worries at all. I appreciate it!
Andrii Sherman
Andrii Sherman14mo ago
So the questions is how to make this in relational queries? just trying to understand exact question
smoke
smoke14mo ago
Yes exactly! If that’s possible I want to fetch all the teams for a specific userId, whether they are connected through the direct ownerId column or through the team_members join table. @Andrii Sherman Did you have time to take a look into this issue? I'd appreciate it!
Andrii Sherman
Andrii Sherman14mo ago
await db.query.teams.findMany({
where: (team, { eq, or }) => or(
eq(team.ownerId, userId),
in(team.teamMembers, userId)
),
});
await db.query.teams.findMany({
where: (team, { eq, or }) => or(
eq(team.ownerId, userId),
in(team.teamMembers, userId)
),
});
and how does this not working for you? seems to be the exact query you need to connect teamMembers to team by one-many, same as I see in your query with leftJoin
smoke
smoke14mo ago
Where would in come from? Or do you mean inArray? Because I can't use team.teamMembers with inArray, that gives me a type error.
Andrii Sherman
Andrii Sherman14mo ago
Now I got it! You just need to make this query
import { or } from "drizzle-orm"

await db.query.teams.findMany({
with: {
members: true
},
});
import { or } from "drizzle-orm"

await db.query.teams.findMany({
with: {
members: true
},
});
where members is a relation defined by relations function for Relational Queries I guess you need to store all members of a team in a teamMembers table. Even if it's an owner. You can still have an ownerId in team table, just for some other purposes Maybe teamMembers table will have a type columns which will be "member" | "owner" With this schema design you don't need to do extra or statements, that will slow down your queries Hope I got you right
smoke
smoke14mo ago
Yes I understand what you mean, that does make more sense honestly and that would also improve the performance. Thanks a lot, I'll just go with that approach. I appreciate it!
Andrii Sherman
Andrii Sherman14mo ago
Sorry to answer for so long, I was just really trying to understand the case Hope it will work for you
smoke
smoke14mo ago
No worries at all, I understand! That will definitely work for me. I thought of that approach first but that would technically allow multiple users to be an owner of a team but I think that will be fine. That's why I had a direct ownerId column on the teams table.
Andrii Sherman
Andrii Sherman14mo ago
You can do either a code level check to not allow more than 1 owner So when adding user to a team, you can always check if team has or has not an ownerId value you can also do a db level constraint. Something like a composite unique for 2 fields (teamId + type('owner')) We don't have such complex indexes in drizzle yet, but you can add this index manually on a migration(or in database itself)
CREATE UNIQUE INDEX some_idx_name ON team_members (team_id, type) WHERE type = 'owner';
CREATE UNIQUE INDEX some_idx_name ON team_members (team_id, type) WHERE type = 'owner';
in this case it won't allow >1 owner for team but will allow >1 member and everything will be on a database level this is a Postgres example but I pretty sure you can find the same for other db's
smoke
smoke14mo ago
I see! That will definitely work I'll try that out, thanks a lot!
Andrii Sherman
Andrii Sherman14mo ago
🫡
smoke
smoke14mo ago
I assume that it is currently not possible to use that ‘or’ condition as in my previous query, using the new relations module?
Want results from more Discord servers?
Add your server
More Posts
Are foreign key polyfills for PlanetScale supported by Drizzle?Hi Prisma supports this (although slow). See: https://www.prisma.io/docs/guides/database/planetscalTS error: Argument of type PgTableWithColumns is not assignable to parameter of type AnyPgTableHi all, after updating to latest i'm getting this TS error. This is my db creation and schema. ``` Many-to-many relational query issuesschema, relations, statements: https://gist.github.com/kylewardnz/37104f989807e96555ea856294a2b670 drizzle-kit: push wants to change column type that hasn't changedjust updated my dependencies: ``` dependencies: - drizzle-orm 0.25.4 + drizzle-orm 0.26.0 devDependedrizzle-kit doesn't seems picking up the default config TS (up:pg)Hi I'd like to report that the latest drizzle-kit `0.18.0` doesn't seems to detect my `drizzle.confBuild queries dynamically with conditions.How to build query on the basis of if-else conditions? I have tried doing it as below ``` let dbQuerdrizzle-kit: Error: Cannot find module 'drizzle-orm/version'Hi there, when I run the following command: `pnpx drizzle-kit generate:pg`, I get the following erroConditional logic within template strings not supported on raw MySQL queryI am trying to execute raw SQL query with some conditional logic but it always fails. It's working fexecuting an `update` statement does not use `.onUpdateNow()` defined in schemaHello everyone 🙂 I have the following schema for my `USERS` table: ```ts export const users = mysqHelp with raw queryThe following query is all raw. Is there a way to make SQL raw only the part of the `where` clause?