sexnine
sexnine
Explore posts from servers
DTDrizzle Team
Created by sexnine on 7/6/2023 in #help
[Relational Queries] Nullable one-to-one relationship
Hey y'all. I'm using the relational queries feature and have a one-to-one relationship between these two tables.
export const teamsT = mysqlTable("Teams", {
id: varchar("id", { length: 21 }).primaryKey(),
name: varchar("name", { length: 40 }).notNull(),
joinCode: varchar("joinCode", { length: 8 }).notNull(),
});

export const projectsT = mysqlTable("Projects", {
id: varchar("id", { length: 21 }).primaryKey(),
teamId: varchar("teamId", { length: 21 }).notNull(),
name: varchar("name", { length: 40 }).notNull(),
description: varchar("description", { length: 255 }).notNull(),
content: text("content").notNull(),
submitted: boolean("submitted").notNull(),
submittedTime: timestamp("submittedTime"),
});

export const teamsRelations = relations(teamsT, ({ one, many }) => ({
project: one(projectsT, {
fields: [teamsT.id],
references: [projectsT.teamId],
}),
}));

export const projectsRelations = relations(projectsT, ({ one, many }) => ({
team: one(teamsT, {
fields: [projectsT.teamId],
references: [teamsT.id],
}),
}));
export const teamsT = mysqlTable("Teams", {
id: varchar("id", { length: 21 }).primaryKey(),
name: varchar("name", { length: 40 }).notNull(),
joinCode: varchar("joinCode", { length: 8 }).notNull(),
});

export const projectsT = mysqlTable("Projects", {
id: varchar("id", { length: 21 }).primaryKey(),
teamId: varchar("teamId", { length: 21 }).notNull(),
name: varchar("name", { length: 40 }).notNull(),
description: varchar("description", { length: 255 }).notNull(),
content: text("content").notNull(),
submitted: boolean("submitted").notNull(),
submittedTime: timestamp("submittedTime"),
});

export const teamsRelations = relations(teamsT, ({ one, many }) => ({
project: one(projectsT, {
fields: [teamsT.id],
references: [projectsT.teamId],
}),
}));

export const projectsRelations = relations(projectsT, ({ one, many }) => ({
team: one(teamsT, {
fields: [projectsT.teamId],
references: [teamsT.id],
}),
}));
I have this query to fetch a user, their team (if they have one) and their team's project (if they have one).
const result = await db.query.registrationsT.findFirst({
where: eq(registrationsT.userId, ctx.user.id),
columns: {
id: true,
},
with: {
team: {
columns: {
id: true,
name: true,
},
with: {
project: {
columns: {
id: true,
},
},
},
},
},
});
const result = await db.query.registrationsT.findFirst({
where: eq(registrationsT.userId, ctx.user.id),
columns: {
id: true,
},
with: {
team: {
columns: {
id: true,
name: true,
},
with: {
project: {
columns: {
id: true,
},
},
},
},
},
});
This is the return type of that function.
{
id: number;
team: { id: string; name: string; project: { id: string } } | null;
} | undefined
{
id: number;
team: { id: string; name: string; project: { id: string } } | null;
} | undefined
The problem is, project could potentially be null if the team does not have a project. I'm not really sure what to do here to tell TS that project can be null. Am I creating the relations correctly or am I doing something wrong? How should I solve this? Thank you in advance for the help and I apologize for the long question.
8 replies