Getting an object instead of an array.

im using Query syntax to get a an array of things based on a relations. the problem is one of the coloums is being sent like this which is wired. postsPicture: { "0": { postId: "352aaabb-8402-4771-829f-f68d563cf275", url: "test", }, "1": { postId: "352aaabb-8402-4771-829f-f68d563cf275", url: "123", }, },
11 Replies
DoggeSlapper
DoggeSlapper4mo ago
what's the code ?
Lucifer
LuciferOP4mo ago
const postsDb = await db.query.posts.findMany({ limit: 2, orderBy: posts.createdAt, with: { comments: true, postsPicture: { columns: { postId: true, url: true, }, }, }, }); the query the problem is only with postPictures schema import { createTable } from "../schema"; import { uuid, varchar } from "drizzle-orm/pg-core"; import { posts } from "./post"; export const postsPicture = createTable("postsPicture", { postId: uuid("postId") .references(() => posts.id, { onDelete: "cascade" }) .notNull(), url: varchar("url", { length: 256 }).notNull(), }); relations export const postsRelations = relations(posts, ({ one, many }) => ({ postsPicture: many(postsPicture), comments: many(comment), likes: many(like), author: one(users, { fields: [posts.userId], references: [users.id], }), })); that is the relevant code
DoggeSlapper
DoggeSlapper4mo ago
it's seems correct to me I'm not sure the return of relations as I'm not using the api but looks fine from docs
Mario564
Mario5644mo ago
@Lucifer I see you defined the relations to the posts table. Have you defined them for postsPictures? If so, can you share it here?
Lucifer
LuciferOP4mo ago
export const picturePostsRelations = relations(postsPicture, ({ one }) => ({ post: one(posts, { fields: [postsPicture.postId], references: [posts.id], }), }));
Mario564
Mario5644mo ago
Looks good I wonder if this is an issue with a particular driver. What driver are you using?
Lucifer
LuciferOP4mo ago
node_postgres. yeah its wired, its giving me an array but as an object. and the comments are a normal array. so its just the postPicture
Mario564
Mario5644mo ago
Are you running the latest version of the ORM?
Lucifer
LuciferOP4mo ago
i think so, 0.33.0
Mario564
Mario5644mo ago
Not sure what exactly is causing this behavior, but I can offer a workaround if you don't mind You could do this:
let postsDb = await /* query */;

postsDb = postsDb.map((posts) => ({
...posts,
postsPicture: Object.values(posts.postsPicture /* in case this errors, add `as any` */)
}));
let postsDb = await /* query */;

postsDb = postsDb.map((posts) => ({
...posts,
postsPicture: Object.values(posts.postsPicture /* in case this errors, add `as any` */)
}));
Lucifer
LuciferOP4mo ago
Oh that was not really an issue. I wrote a work around. Just wanted to figure out why is that. But thank you!
Want results from more Discord servers?
Add your server