AlcaponeYou
AlcaponeYou
DTDrizzle Team
Created by Ꚃimon on 8/9/2023 in #help
Nullable relational query?
Sorry this is entirely my fault. The error I got wasa b/c I forgot to export the reference model. Relational queries on nullable fields are working.
15 replies
DTDrizzle Team
Created by Ꚃimon on 8/9/2023 in #help
Nullable relational query?
Awesome, looking forward to it. Do you have an ETA on the release?
15 replies
DTDrizzle Team
Created by Ꚃimon on 8/9/2023 in #help
Nullable relational query?
@Dan Kochetov hi, is it possible to use the relations on a nullable field? When I try to query using with: { maybeNull: true }, it throws an error: Cannot read properties of undefined (reading 'column's). When maybeNull isn't null, the query works.
15 replies
DTDrizzle Team
Created by kraustifer on 8/15/2023 in #help
Count of one-to-many relation in query
eg:
someRelationalQuery: {
columns: {...},
with: {
something: true
},
extras: {
count: sql`(SELECT count(*) from relation_table)`.as('count')
}
}
someRelationalQuery: {
columns: {...},
with: {
something: true
},
extras: {
count: sql`(SELECT count(*) from relation_table)`.as('count')
}
}
Note you have to wrap the query in parenthesis.
5 replies
DTDrizzle Team
Created by kraustifer on 8/15/2023 in #help
Count of one-to-many relation in query
Oh wait it's already supported, the docs just aren't updated.
5 replies
DTDrizzle Team
Created by kraustifer on 8/15/2023 in #help
Count of one-to-many relation in query
I'm curious if this will be supported in the future too.
5 replies
DTDrizzle Team
Created by Skyler on 6/4/2023 in #help
Many to many relationship between one type
np, did you figure out what you did wrong before
20 replies
DTDrizzle Team
Created by Skyler on 6/4/2023 in #help
Many to many relationship between one type
and an example query:
export async function getFriends(id: string) {
return config.db.query.users.findFirst({
where: eq(users.id, id),
columns: {},
with: {
followers: {
columns: {},
where: sql`${relations.status} & ${
RelationsStatus.Friend | RelationsStatus.Followed
}`,
with: {
friend: true,
},
},
followings: {
columns: {},
where: sql`${relations.status} & ${
RelationsStatus.Friend | RelationsStatus.Followed
}`,
with: {
user: true,
},
},
},
})
}
export async function getFriends(id: string) {
return config.db.query.users.findFirst({
where: eq(users.id, id),
columns: {},
with: {
followers: {
columns: {},
where: sql`${relations.status} & ${
RelationsStatus.Friend | RelationsStatus.Followed
}`,
with: {
friend: true,
},
},
followings: {
columns: {},
where: sql`${relations.status} & ${
RelationsStatus.Friend | RelationsStatus.Followed
}`,
with: {
user: true,
},
},
},
})
}
20 replies
DTDrizzle Team
Created by Skyler on 6/4/2023 in #help
Many to many relationship between one type
Instead of a "followings" table, I have a "relations" table that describes friendship + followerships, but the idea is the same: users:
export const usersRelations = ormRelations(users, ({ many }) => ({
followers: many(relations, { relationName: RELATION_NAMES.FOLLOWERS }),
followings: many(relations, { relationName: RELATION_NAMES.FOLLOWINGS }),
}))
export const usersRelations = ormRelations(users, ({ many }) => ({
followers: many(relations, { relationName: RELATION_NAMES.FOLLOWERS }),
followings: many(relations, { relationName: RELATION_NAMES.FOLLOWINGS }),
}))
relations:
export const relations = sqliteTable(
'relations',
{
userId: text('user_id')
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
friendId: text('friend_id')
.references(() => users.id, {
onDelete: 'cascade',
})
.notNull(),
status: int('status').default(RelationsStatus.RequestedFriend).notNull(),
},
(table) => ({
pk: primaryKey(table.userId, table.friendId),
user: index('relations_user_idx').on(table.userId),
friend: index('relations_friend_idx').on(table.friendId),
})
)
export const relationsRelations = ormRelations(relations, ({ one }) => ({
user: one(users, {
fields: [relations.userId],
references: [users.id],
relationName: RELATION_NAMES.FOLLOWERS,
}),
friend: one(users, {
fields: [relations.friendId],
references: [users.id],
relationName: RELATION_NAMES.FOLLOWINGS,
}),
}))
export const relations = sqliteTable(
'relations',
{
userId: text('user_id')
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
friendId: text('friend_id')
.references(() => users.id, {
onDelete: 'cascade',
})
.notNull(),
status: int('status').default(RelationsStatus.RequestedFriend).notNull(),
},
(table) => ({
pk: primaryKey(table.userId, table.friendId),
user: index('relations_user_idx').on(table.userId),
friend: index('relations_friend_idx').on(table.friendId),
})
)
export const relationsRelations = ormRelations(relations, ({ one }) => ({
user: one(users, {
fields: [relations.userId],
references: [users.id],
relationName: RELATION_NAMES.FOLLOWERS,
}),
friend: one(users, {
fields: [relations.friendId],
references: [users.id],
relationName: RELATION_NAMES.FOLLOWINGS,
}),
}))
20 replies
DTDrizzle Team
Created by Skyler on 6/4/2023 in #help
Many to many relationship between one type
Yes I'll be home in about an hour
20 replies
DTDrizzle Team
Created by Skyler on 6/4/2023 in #help
Many to many relationship between one type
Uncomment the blocks
20 replies
DTDrizzle Team
Created by Skyler on 6/4/2023 in #help
Many to many relationship between one type
nvm got this working now, I had a typo in the relationName (extra s in followings)
20 replies
DTDrizzle Team
Created by Skyler on 6/4/2023 in #help
Many to many relationship between one type
@minion3665 did you get this working? In your example above, you have Following = pgTable("followers", { the mismatch between Following and followers is confusing. Did you also have a separate "followings" table?
20 replies
DTDrizzle Team
Created by b0o on 6/27/2023 in #help
Why is insertId a string, not a number?
oh you're trying to get the inserted data. I forgot mysql doesn't have returning. I wonder if drizzle has .returning() for mysql. I'm using sqlite and I'm able to do returnining().get() to retrieve the new inserted data
10 replies
DTDrizzle Team
Created by b0o on 6/27/2023 in #help
Why is insertId a string, not a number?
can you post the schema of the table, and the ts code too. I've made mistakes where I duplicated the table column name which overrides the pk.
10 replies
DTDrizzle Team
Created by Jim on 6/26/2023 in #help
TypeError when adding subquery to `drizzle.insert.values`
might be a bug w/ drizzle. I haven't tried subquery on insert yet
6 replies
DTDrizzle Team
Created by Jim on 6/26/2023 in #help
TypeError when adding subquery to `drizzle.insert.values`
6 replies
DTDrizzle Team
Created by nikivi on 6/26/2023 in #help
Get issue trying to run libsql/turso example
then rerun
147 replies
DTDrizzle Team
Created by nikivi on 6/26/2023 in #help
Get issue trying to run libsql/turso example
and topics
147 replies
DTDrizzle Team
Created by nikivi on 6/26/2023 in #help
Get issue trying to run libsql/turso example
drop table users;
147 replies