Marcel
Marcel
DTDrizzle Team
Created by Marcel on 2/22/2025 in #help
mapWith not running in Drizzle queries (db.query)
Hello, not sure if this is the best place to post this (please let me know if I should follow-up on this issue someplace else). I believe there's a bug causing mapWith to not run in Drizzle queries (db.query) when using certain native SQL functions. SQLite example: https://drizzle.run/n509hxyyy5prp2roiyjs6em6 In this example, json_object is returned as a stringified object unless I use the traditional query syntax. I have added mapWith(() => "a") to make it obvious that mapWith is not running. PostgreSQL example: https://drizzle.run/d898kcgfpmzfhdewjnyn4jdp In this example, json_build_object is returned correctly as an actual object. However, mapWith is not running unless I use the traditional query syntax. I have added mapWith(() => "a") to make it obvious that mapWith is not running. These examples are very minimal but I'm running into this issue in a production app where I use a function that constructs nested subqueries with dynamic columns and returns the subquery data as JSON (or a stringified JSON in SQLite if I use the Drizzle query syntax).
1 replies
DTDrizzle Team
Created by Marcel on 10/25/2024 in #help
How to declare optional one-to-one relationship
I have 2 tables that relate to each other in an inverse manner:
export const users = createTable('users', {
id: text('id').primaryKey(),
email: text('email').notNull().unique(),
})

export const profiles = createTable('profiles', {
userId: text('user_id')
.notNull()
.primaryKey()
.references(() => users.id),
firstName: text('first_name').notNull(),
lastName: text('last_name').notNull(),
})

export const usersRelations = relations(users, ({ one }) => ({
profile: one(profiles, {
fields: [users.id],
references: [profiles.userId],
}),
}))

export const profilesRelations = relations(profiles, ({ one }) => ({
user: one(users, {
fields: [profiles.userId],
references: [users.id]
}),
}))
export const users = createTable('users', {
id: text('id').primaryKey(),
email: text('email').notNull().unique(),
})

export const profiles = createTable('profiles', {
userId: text('user_id')
.notNull()
.primaryKey()
.references(() => users.id),
firstName: text('first_name').notNull(),
lastName: text('last_name').notNull(),
})

export const usersRelations = relations(users, ({ one }) => ({
profile: one(profiles, {
fields: [users.id],
references: [profiles.userId],
}),
}))

export const profilesRelations = relations(profiles, ({ one }) => ({
user: one(users, {
fields: [profiles.userId],
references: [users.id]
}),
}))
If I go ahead and create a user without a profile in Studio, no error is shown. However, when I run the following script during runtime, unexpected errors may appear.
const user = await db.query.users.findFirst({
with: {
profile: {
columns: { firstName: true }
}
},
})

user //? { id: string; email: string; profile: { firstName: string } } | undefined

console.log(user?.profile.firstName) // No TypeScript error appears because it assumes profile always exists

// Runtime error: null is not an object (evaluating 'user?.profile.firstName')
const user = await db.query.users.findFirst({
with: {
profile: {
columns: { firstName: true }
}
},
})

user //? { id: string; email: string; profile: { firstName: string } } | undefined

console.log(user?.profile.firstName) // No TypeScript error appears because it assumes profile always exists

// Runtime error: null is not an object (evaluating 'user?.profile.firstName')
I understand that I could fix this by updating my schema relationships and linking a profile ID in the users table but, given that I have many tables that relate to individual users where table information may or may not exist (e.g. profile, KYC data, KYB data, etc.), I thought this would be the best way to design it.
2 replies
DTDrizzle Team
Created by Marcel on 8/17/2024 in #help
[Turso] Undefined primaryKey on push
How can I migrate a schema using Turso (libsql)? When I run drizzle-kit push, I get this error: Cannot read properties of undefined (reading 'primaryKey') I have also tried to introspect and generate the new SQL script, but I get this error: LibsqlError: SQL_NO_STATEMENT: SQL string does not contain any statement. Is there a way to fix this? Not sure what I should do as a first-time drizzle user
3 replies