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
what's the code ?
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 codeit'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
@Lucifer I see you defined the relations to the posts table. Have you defined them for postsPictures? If so, can you share it here?
export const picturePostsRelations = relations(postsPicture, ({ one }) => ({
post: one(posts, {
fields: [postsPicture.postId],
references: [posts.id],
}),
}));
Looks good
I wonder if this is an issue with a particular driver. What driver are you using?
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
Are you running the latest version of the ORM?
i think so, 0.33.0
Not sure what exactly is causing this behavior, but I can offer a workaround if you don't mind
You could do this:
Oh that was not really an issue. I wrote a work around. Just wanted to figure out why is that. But thank you!