Anyone have any idea about this bug?

GitHub
[BUG]: Relations shouldn't enforce array values · Issue #1816 · dri...
What version of drizzle-orm are you using? 0.29.3 What version of drizzle-kit are you using? 0.20.9 Describe the Bug export const table1 = pgTable( 'Table1', { id: text('id').primar...
9 Replies
jakeleventhal
jakeleventhalOP10mo ago
How else are you to make that kind of query
George
George10mo ago
@jakeleventhal what are you trying to do? 1) table1Relations is missing a column in fields and references 2) table2Relations does not have a table1ItemOne so typescript is correct in saying it doesn't exist
jakeleventhal
jakeleventhalOP10mo ago
my specific use case is as follows: a user is able to add a design for a package sticker as well as a package insert. designs can also be used for other things so in my use case i have designs.packageStickerSettingId and designs.packageInsertSettingsId. essentially what i want to be able to do is query the user's settings along with the deisgn values for packageSticker and packageInsert by doing a query like
db.query.settings.findFirst({ with: { packageSticker: true, packageInsert: true } })
db.query.settings.findFirst({ with: { packageSticker: true, packageInsert: true } })
George
George10mo ago
would you mind providing the tables/relations with the actual names you're using? feel free to strip down irrelevant columns
jakeleventhal
jakeleventhalOP10mo ago
sure, here is the prisma version of the tables that was working:
model Settings {
id String @id @default(uuid())

packageInsert Design? @relation(name: "packageInsert")
packageSticker Design?
}

model Design {
id String @id @default(uuid())

packageInsert Settings? @relation(fields: [packageInsertId], references: [id], onDelete: Cascade, "packageInsert")
packageInsertId String? @unique
packageSticker Settings? @relation(fields: [packageStickerId], references: [id], onDelete: Cascade)
packageStickerId String? @unique
}
model Settings {
id String @id @default(uuid())

packageInsert Design? @relation(name: "packageInsert")
packageSticker Design?
}

model Design {
id String @id @default(uuid())

packageInsert Settings? @relation(fields: [packageInsertId], references: [id], onDelete: Cascade, "packageInsert")
packageInsertId String? @unique
packageSticker Settings? @relation(fields: [packageStickerId], references: [id], onDelete: Cascade)
packageStickerId String? @unique
}
(aka i could do this query)
prisma.settings.findFirst({ include: { packageSticker: true, packageInsert: true } })
prisma.settings.findFirst({ include: { packageSticker: true, packageInsert: true } })
here is my drizzle rendition
export const settings = pgTable('Settings', {
id: text('id')
.primaryKey()
.$defaultFn(() => sql`gen_random_uuid()::text`)
.notNull()
});

export const designs = pgTable(
'Design',
{
id: text('id')
.primaryKey()
.$defaultFn(() => sql`gen_random_uuid()::text`)
.notNull(),
packageInsertId: text('packageInsertId').references(() => settings.id, {
onDelete: 'cascade',
onUpdate: 'cascade'
}),
packageStickerId: text('packageStickerId').references(() => settings.id, {
onDelete: 'cascade',
onUpdate: 'cascade'
})
},
(table) => ({
packageInsertIdKey: uniqueIndex('Design_packageInsertId_key').on(table.packageInsertId),
packageStickerIdKey: uniqueIndex('Design_packageStickerId_key').on(table.packageStickerId)
})
);
export const settings = pgTable('Settings', {
id: text('id')
.primaryKey()
.$defaultFn(() => sql`gen_random_uuid()::text`)
.notNull()
});

export const designs = pgTable(
'Design',
{
id: text('id')
.primaryKey()
.$defaultFn(() => sql`gen_random_uuid()::text`)
.notNull(),
packageInsertId: text('packageInsertId').references(() => settings.id, {
onDelete: 'cascade',
onUpdate: 'cascade'
}),
packageStickerId: text('packageStickerId').references(() => settings.id, {
onDelete: 'cascade',
onUpdate: 'cascade'
})
},
(table) => ({
packageInsertIdKey: uniqueIndex('Design_packageInsertId_key').on(table.packageInsertId),
packageStickerIdKey: uniqueIndex('Design_packageStickerId_key').on(table.packageStickerId)
})
);
i didnt include the relations becuase i couldnt get them to work but what i was trying to do was basically this
export const settingsRelations = relations(settings, ({ one }) => ({
packageInsert: one(designs),
packageSticker: one(designs)
}));

export const designRelations = relations(designs, ({ one }) => ({
packageInsertSettings: one(settings, {
fields: [designs.packageInsertId],
references: [settings.id]
}),
packageStickerSettings: one(settings, {
fields: [designs.packageStickerId],
references: [settings.id]
})
}));
export const settingsRelations = relations(settings, ({ one }) => ({
packageInsert: one(designs),
packageSticker: one(designs)
}));

export const designRelations = relations(designs, ({ one }) => ({
packageInsertSettings: one(settings, {
fields: [designs.packageInsertId],
references: [settings.id]
}),
packageStickerSettings: one(settings, {
fields: [designs.packageStickerId],
references: [settings.id]
})
}));
George
George10mo ago
export const settingsRelations = relations(settings, ({ one }) => ({
packageInsert: one(designs, {
fields: [settings.id],
references: [designs.packageInsertId],
relationName: "packageInsert",
}),
packageSticker: one(designs, {
fields: [settings.id],
references: [designs.packageStickerId],
relationName: "packageSticker",
}),
}));

export const designRelations = relations(designs, ({ one }) => ({
packageInsertSettings: one(settings, {
fields: [designs.packageInsertId],
references: [settings.id],
relationName: "packageInsert",
}),
packageStickerSettings: one(settings, {
fields: [designs.packageStickerId],
references: [settings.id],
relationName: "packageSticker",
}),
}));
export const settingsRelations = relations(settings, ({ one }) => ({
packageInsert: one(designs, {
fields: [settings.id],
references: [designs.packageInsertId],
relationName: "packageInsert",
}),
packageSticker: one(designs, {
fields: [settings.id],
references: [designs.packageStickerId],
relationName: "packageSticker",
}),
}));

export const designRelations = relations(designs, ({ one }) => ({
packageInsertSettings: one(settings, {
fields: [designs.packageInsertId],
references: [settings.id],
relationName: "packageInsert",
}),
packageStickerSettings: one(settings, {
fields: [designs.packageStickerId],
references: [settings.id],
relationName: "packageSticker",
}),
}));
perhaps? although in your prisma schema the relations are for the Branding table?
jakeleventhal
jakeleventhalOP10mo ago
settings table is actually branding, just named settings to make it easier to understand in this example. i updated my message above your solution actually works!
George
George10mo ago
great :D
jakeleventhal
jakeleventhalOP10mo ago
ty
Want results from more Discord servers?
Add your server