one-to-many

I might be asking something simple or have to change my model but I thought I'd ask cause I've been stuck for a while. I have an images table
export const images = pgTable('images', {
id: text('id')
.$defaultFn(() => `image_${nanoid()}`)
.primaryKey(),
label: text('label'),
url: text('url'),
resourceId: text('resource_id'),
resourceType: text('resource_type'),
createdAt: timestamp('created_at').defaultNow().notNull(),
});
export const images = pgTable('images', {
id: text('id')
.$defaultFn(() => `image_${nanoid()}`)
.primaryKey(),
label: text('label'),
url: text('url'),
resourceId: text('resource_id'),
resourceType: text('resource_type'),
createdAt: timestamp('created_at').defaultNow().notNull(),
});
you can see I have a resourceId and a resourceType because I have different things that can have relations to images. I know I could create a table inbetween but I thought I'd try there, where I'm stuck is on setting up the relation. All the documentation shows
many(images)
many(images)
But when I run it I get an error on the resource I'm doing the with as "There is not enough information to infer the relation" Any suggestions?
3 Replies
Angelelz
Angelelz15mo ago
You have to set up the relation on both sides In the table that has the images you set up a many(images) In the images table, you set up the one(whatever_table_name_uses_images)
volcanicislander
volcanicislander15mo ago
And try to add a relation name (the same) on both sides as well. Like many(images, { relationName: "resources.image" }) or something like that
Angelelz
Angelelz15mo ago
Yeah
Want results from more Discord servers?
Add your server