Relational query error: "Cannot read properties of null (reading 'map')"

Hi, have an issue and was wondering if anyone has stumbled upon a similar thing? When doing a query like:
findWorkOrderById(id: number): Promise<WorkOrderEntity | undefined> {
return this.db.instance.query.gisTasks.findFirst({
where: eq(gisTasks.id, id),
columns: {
assignedToId: false,
},
with: {
gisAttachments: {
with: {
miUser: {
columns: {
id: true,
forename: true,
lastname: true,
},
},
},
},
// ...other relations that works
},
})
}
findWorkOrderById(id: number): Promise<WorkOrderEntity | undefined> {
return this.db.instance.query.gisTasks.findFirst({
where: eq(gisTasks.id, id),
columns: {
assignedToId: false,
},
with: {
gisAttachments: {
with: {
miUser: {
columns: {
id: true,
forename: true,
lastname: true,
},
},
},
},
// ...other relations that works
},
})
}
I get the following error : (see pic for full error) The Cannot read properties of null (reading 'map') at Object.mapRelationalRow.... error appears when trying to make a relational one-to-many queries. The thing is, the error only happens when there is no gisAttachments with reference id to the workorder. But when there is one it works. I expect it to return an empty array, but drizzle throws this error. Relation definition:
export const gisAttachmentsRelations = relations(gisAttachments, ({ one }) => ({
task: one(gisTasks, {
fields: [gisAttachments.taskId],
references: [gisTasks.id],
}),
miUser: one(miUsers, {
fields: [gisAttachments.userId],
references: [miUsers.id],
}),
}))
export const gisTasksRelations = relations(gisTasks, ({ one, many }) => ({
gisAttachments: many(gisAttachments),
assignedTo: one(miUsers, {
fields: [gisTasks.assignedToId],
references: [miUsers.id],
}),
// other 'one' relation definitions
}))
export const gisAttachmentsRelations = relations(gisAttachments, ({ one }) => ({
task: one(gisTasks, {
fields: [gisAttachments.taskId],
references: [gisTasks.id],
}),
miUser: one(miUsers, {
fields: [gisAttachments.userId],
references: [miUsers.id],
}),
}))
export const gisTasksRelations = relations(gisTasks, ({ one, many }) => ({
gisAttachments: many(gisAttachments),
assignedTo: one(miUsers, {
fields: [gisTasks.assignedToId],
references: [miUsers.id],
}),
// other 'one' relation definitions
}))
Thank you in advance for the assistance!
No description
2 Replies
Mykhailo
Mykhailo11mo ago
Hello, @strandhvilliam! Could you please provide your full schema and drizzle-orm version? I tested with my similar code and can't reproduce the issue.
Villiam Strandh
Villiam StrandhOP10mo ago
export const gisTasks = mysqlTable(
'gis_tasks',
{
id: int('ID').autoincrement().notNull(),
siteNumber: tinyint('SiteNumber').default(1).notNull(),
status: tinyint('Status').default(0).notNull(),
createdDate: datetime('CreatedDate', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
assignedTo: int('AssignedTo').notNull(),
updatedDate: datetime('UpdatedDate', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
},
table => {
return {
gisTasksId: primaryKey(table.id),
}
}
)

export const gisAttachments = mysqlTable(
'gis_attachments',
{
id: int('ID').autoincrement().notNull(),
taskId: int('TaskID').notNull(),
updatedDate: datetime('UpdatedDate', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
userId: int('UserID').default(0).notNull(),
type: int('Type').notNull(),
data: varchar('Data', { length: 255 }).notNull(),
},
table => {
return {
fkGisAttachmentTask: index('FK_gis_attachment_task').on(table.taskId),
gisAttachmentsId: primaryKey(table.id),
}
}
)
export const gisTasks = mysqlTable(
'gis_tasks',
{
id: int('ID').autoincrement().notNull(),
siteNumber: tinyint('SiteNumber').default(1).notNull(),
status: tinyint('Status').default(0).notNull(),
createdDate: datetime('CreatedDate', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
assignedTo: int('AssignedTo').notNull(),
updatedDate: datetime('UpdatedDate', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
},
table => {
return {
gisTasksId: primaryKey(table.id),
}
}
)

export const gisAttachments = mysqlTable(
'gis_attachments',
{
id: int('ID').autoincrement().notNull(),
taskId: int('TaskID').notNull(),
updatedDate: datetime('UpdatedDate', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
userId: int('UserID').default(0).notNull(),
type: int('Type').notNull(),
data: varchar('Data', { length: 255 }).notNull(),
},
table => {
return {
fkGisAttachmentTask: index('FK_gis_attachment_task').on(table.taskId),
gisAttachmentsId: primaryKey(table.id),
}
}
)
Thank you @solo ! Here is the relevant schema code. We are currently running "drizzle-kit": "^0.19.13", and "drizzle-orm": "^0.28.6".
Want results from more Discord servers?
Add your server