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
DoggeSlapper2d ago
what's the code ?
Lucifer
Lucifer2d 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
DoggeSlapper2d 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
Mario5642d 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
Lucifer2d ago
export const picturePostsRelations = relations(postsPicture, ({ one }) => ({ post: one(posts, { fields: [postsPicture.postId], references: [posts.id], }), }));
Mario564
Mario5642d ago
Looks good I wonder if this is an issue with a particular driver. What driver are you using?
Lucifer
Lucifer2d 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
Mario5642d ago
Are you running the latest version of the ORM?
Lucifer
Luciferthis hour
i think so, 0.33.0
Mario564
Mario564this hour
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
Lucifer22h 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