T2ThatGuy
T2ThatGuy
DTDrizzle Team
Created by T2ThatGuy on 10/15/2023 in #help
Bigint showing incorrect value from db
I have the following schema
export const guild = pgTable('Guild', {
id: bigint('id', { mode: 'bigint' }).primaryKey().notNull(),
name: text('name').notNull(),
main: boolean('main').default(false).notNull(),
});

export const guildPartyConfig = pgTable('GuildPartyConfig', {
id: serial('id').primaryKey().notNull(),
partyChannelId: bigint('partyChannelId', { mode: 'bigint' }),
raidRoleId: bigint('raidRoleId', { mode: 'bigint' }),
partyRoleId: bigint('partyRoleId', { mode: 'bigint' }),
dungeonRoleId: bigint('dungeonRoleId', { mode: 'bigint' }),
receiveBroadcasts: boolean('receiveBroadcasts').default(true),
onDelete: boolean('onDelete').default(true),
guildId: bigint('guildId', { mode: 'bigint' }).references(() => guild.id),
});

export const guildRelations = relations(guild, ({ one }) => ({
partyConfig: one(guildPartyConfig, {
fields: [guild.id],
references: [guildPartyConfig.guildId],
}),
}));
export const guild = pgTable('Guild', {
id: bigint('id', { mode: 'bigint' }).primaryKey().notNull(),
name: text('name').notNull(),
main: boolean('main').default(false).notNull(),
});

export const guildPartyConfig = pgTable('GuildPartyConfig', {
id: serial('id').primaryKey().notNull(),
partyChannelId: bigint('partyChannelId', { mode: 'bigint' }),
raidRoleId: bigint('raidRoleId', { mode: 'bigint' }),
partyRoleId: bigint('partyRoleId', { mode: 'bigint' }),
dungeonRoleId: bigint('dungeonRoleId', { mode: 'bigint' }),
receiveBroadcasts: boolean('receiveBroadcasts').default(true),
onDelete: boolean('onDelete').default(true),
guildId: bigint('guildId', { mode: 'bigint' }).references(() => guild.id),
});

export const guildRelations = relations(guild, ({ one }) => ({
partyConfig: one(guildPartyConfig, {
fields: [guild.id],
references: [guildPartyConfig.guildId],
}),
}));
For some reason with partyChannelId set to 998290192232362024 inside the database. When it is queried with the code below in the prepared statement, the ID is read incorrectly and as a result appears as 998290192232361984 during runtime.
drizzle.query.guild
.findMany({ with: { partyConfig: true } })
.prepare('initalGuilds');
drizzle.query.guild
.findMany({ with: { partyConfig: true } })
.prepare('initalGuilds');
Am I using the query functionality incorrect as when queried directly or without utilizing the query method it gets the correct result during runtime, with the value of 998290192232362024 as expected.
drizzle.query.guildPartyConfig.findMany()

// Either Work Fine
drizzle.select({ id: guild.id, name: guild.name, partyConfig: guildPartyConfig }).from(guild).leftJoin(guildPartyConfig, eq(guild.id, guildPartyConfig.guildId))
drizzle.query.guildPartyConfig.findMany()

// Either Work Fine
drizzle.select({ id: guild.id, name: guild.name, partyConfig: guildPartyConfig }).from(guild).leftJoin(guildPartyConfig, eq(guild.id, guildPartyConfig.guildId))
Again I will ask am I using the query API incorrectly or is this a bug?
3 replies
DTDrizzle Team
Created by T2ThatGuy on 8/24/2023 in #help
Conditional Relational Queries
I have these tables Simplified Version
export const ingredientTypeEnum = pgEnum('ingredientType', ['ITEM', 'FOOD']);

export const food = pgTable('food', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
});

export const item = pgTable('item', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
description: text('description').array(),
});

export const ingredient = pgTable('ingredient', {
recipe_id: integer('recipe_id').references(() => recipe.id),
ingredientType: ingredientTypeEnum('ingredientType'),
quantity: integer('amount').notNull(),
});

export const recipe = pgTable('recipe', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
instructions: text('description').array(),
});

export const recipeRelations = relations(recipe, ({ many }) => ({
ingredients: many(ingredient),
}));
export const ingredientTypeEnum = pgEnum('ingredientType', ['ITEM', 'FOOD']);

export const food = pgTable('food', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
});

export const item = pgTable('item', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
description: text('description').array(),
});

export const ingredient = pgTable('ingredient', {
recipe_id: integer('recipe_id').references(() => recipe.id),
ingredientType: ingredientTypeEnum('ingredientType'),
quantity: integer('amount').notNull(),
});

export const recipe = pgTable('recipe', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
instructions: text('description').array(),
});

export const recipeRelations = relations(recipe, ({ many }) => ({
ingredients: many(ingredient),
}));
Is it possible to setup the table relationships so that depending on the ingredient type it utilizes the corresponding tables? For example, with an ingredient type of ITEM it would use the item table instead of the food table.
7 replies