chris_st
Explore posts from serversDTDrizzle Team
•Created by chris_st on 10/19/2024 in #help
Problem with findMany with many-to-many relations
First, here's my schema file:
import { integer, primaryKey, sqliteTable, text } from 'drizzle-orm/sqlite-core'
import { relations } from 'drizzle-orm'
export const Entries = sqliteTable('Entries', {
Id: integer('Id', { mode: 'number' }).primaryKey({ autoIncrement: true }),
PersonId: integer('PersonId', { mode: 'number' }).notNull(),
Content: text('Content').notNull(),
Timestamp: text('Timestamp').notNull(),
})
export const EntriesRelations = relations(Entries, ({ many }) => ({
EntriesTags: many(EntriesTags),
}))
export const Tags = sqliteTable('Tags', {
Id: integer('Id', { mode: 'number' }).primaryKey({ autoIncrement: true }),
Tag: text('Tag').notNull(),
})
export const TagsRelations = relations(Tags, ({ many }) => ({
EntriesTags: many(EntriesTags),
}))
export const EntriesTags = sqliteTable(
'EntriesTags',
{
EntryId: integer('EntryId', { mode: 'number' })
.notNull()
.references(() => Entries.Id),
TagId: integer('TagId', { mode: 'number' })
.notNull()
.references(() => Tags.Id),
},
(t) => ({
pk: primaryKey({ columns: [t.EntryId, t.TagId] }),
})
)
export const EntriesTagsRelations = relations(
EntriesTags,
({ one }) => ({
Entry: one(Entries, {
fields: [EntriesTags.EntryId],
references: [Entries.Id],
}),
Tag: one(Tags, {
fields: [EntriesTags.TagId],
references: [Tags.Id],
}),
})
)
3 replies
DTDrizzle Team
•Created by chris_st on 10/15/2024 in #help
Help with 'in' queries
Hi - sorry if this has been asked, but it's kind of hard to search for!
If it helps, I'm using cloudflare's D1 database (sqlite).
What I'd like to do is the following query:
select id, tag from tags where tag in ('foo', 'bar', 'baz');
The closest I can get so far is:
const tagsAlreadyThere = await getEntriesDb(context).run(
Select Id, Tag from LFLGTags where Tag in (${tags.map((t) =>
'${t}').join(', ')})
)
But that fails if a tag has a single quote in it... I'd like to do something like a prepare
statement, but I haven't been able to figure out how to do that in this case.
If there's a better way to do such a query I'd be glad to find it... the nice thing about it is it's a single query, rather than querying for each tag individually.
Thanks!2 replies