DT
Drizzle Team•13mo ago
Hazymoon

Can not access relation object with 2 many relations

I'm declaring the relations of a table like this:
export const storesToStoreLocationsRelation = relations(Stores, ({ many }) => ({
storeLocations: many(StoreLocations),
}));

export const storesToLocationsRelation = relations(Stores, ({ many }) => ({
stackLocations: many(StackLocations),
}));
export const storesToStoreLocationsRelation = relations(Stores, ({ many }) => ({
storeLocations: many(StoreLocations),
}));

export const storesToLocationsRelation = relations(Stores, ({ many }) => ({
stackLocations: many(StackLocations),
}));
The reverse relation:
export const storeLocationsToStoresRelation = relations(StoreLocations, ({ one }) => ({
stores: one(Stores, {
fields: [StoreLocations.storeId],
references: [Stores.id],
}),
}));
export const storeLocationsToStoresRelation = relations(StoreLocations, ({ one }) => ({
stores: one(Stores, {
fields: [StoreLocations.storeId],
references: [Stores.id],
}),
}));
I can make this query:
const result = await db2.query.Stores.findMany({
with: {
storeLocations: {
with: {
stores: true,
},
},
},
});
const result = await db2.query.Stores.findMany({
with: {
storeLocations: {
with: {
stores: true,
},
},
},
});
but when I access result[0].storeLocations , it is not suggested and it says Property storeLocations does not exist.... But if I do console.info(result[0]) I can see storeLocations inside the object. If I remove the other many relation, then it works normally.
2 Replies
Angelelz
Angelelz•13mo ago
All relation of the same table have to be declared in the same object. Also when you have more that one relation with the same table, you need to give each a relationName so that drizzle can differentiate them. The name have to match in both sides of the relation
Hazymoon
HazymoonOP•13mo ago
Thank you so much!. It works now 🙌
Want results from more Discord servers?
Add your server