query with

// schema
export const usersTable = sqliteTable("users", {
id: text("id").primaryKey(),
username: text("username"),
// otros campos...
});

export const followsTable = sqliteTable(
"follows",
{
followerId: text("follower_id").references(() => usersTable.id),
followingId: text("following_id").references(() => usersTable.id),
createdAt: text("created_at").default(sql`(CURRENT_TIMESTAMP)`),
},
(followsTable) => ({
id: primaryKey([followsTable.followerId, followsTable.followingId]),
})
);
// schema
export const usersTable = sqliteTable("users", {
id: text("id").primaryKey(),
username: text("username"),
// otros campos...
});

export const followsTable = sqliteTable(
"follows",
{
followerId: text("follower_id").references(() => usersTable.id),
followingId: text("following_id").references(() => usersTable.id),
createdAt: text("created_at").default(sql`(CURRENT_TIMESTAMP)`),
},
(followsTable) => ({
id: primaryKey([followsTable.followerId, followsTable.followingId]),
})
);
I'm trying to do this query, but "with" doesn't recommend anything, did I link my schema wrong?
import { db } from "../../../db/db";
import { EdarErr } from "../../../error/EdarErr";
import { Uuid } from "../../../types";

export const getAccountService = async (id: Uuid) => {
const account = await db.query.usersTable.findFirst({
where: (user, { eq }) => eq(user.id, id),
with: { ?: true },
});

if (!account) throw new EdarErr(404, "Account not found");

return account;
};
import { db } from "../../../db/db";
import { EdarErr } from "../../../error/EdarErr";
import { Uuid } from "../../../types";

export const getAccountService = async (id: Uuid) => {
const account = await db.query.usersTable.findFirst({
where: (user, { eq }) => eq(user.id, id),
with: { ?: true },
});

if (!account) throw new EdarErr(404, "Account not found");

return account;
};
8 Replies
rphlmr ⚡
rphlmr ⚡4mo ago
You need to define relations for “query” api. Did you? https://orm.drizzle.team/docs/rqb
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
edarcode
edarcodeOP4mo ago
mmm let me check It is not enough to establish the relationship in the schema? Do I need to do something else?
edarcode
edarcodeOP4mo ago
haha
No description
edarcode
edarcodeOP4mo ago
export const usersTableRelations = relations(usersTable, ({ many }) => ({
usersToFollows: many(followsTable),
}));


export const followsTableRelations = relations(followsTable, ({ many }) => ({
followsToUsers: many(usersTable),
}));
export const usersTableRelations = relations(usersTable, ({ many }) => ({
usersToFollows: many(followsTable),
}));


export const followsTableRelations = relations(followsTable, ({ many }) => ({
followsToUsers: many(usersTable),
}));
after
import { db } from "../../../db/db";
import { EdarErr } from "../../../error/EdarErr";
import { Uuid } from "../../../types";

export const getAccountService = async (id: Uuid) => {
const account = await db.query.usersTable.findFirst({
where: (user, { eq }) => eq(user.id, id),
with: { usersToFollows: true },
});

if (!account) throw new EdarErr(404, "Account not found");

return account;
};
import { db } from "../../../db/db";
import { EdarErr } from "../../../error/EdarErr";
import { Uuid } from "../../../types";

export const getAccountService = async (id: Uuid) => {
const account = await db.query.usersTable.findFirst({
where: (user, { eq }) => eq(user.id, id),
with: { usersToFollows: true },
});

if (!account) throw new EdarErr(404, "Account not found");

return account;
};
edarcode
edarcodeOP4mo ago
but
No description
edarcode
edarcodeOP4mo ago
If I remove with it works, if I put with it doesn't work
⚡Z.E.U.S⚡
⚡Z.E.U.S⚡4mo ago
@edarcode Hey! Are you still having this error in Drizzle Studio?
edarcode
edarcodeOP4mo ago
That same day I closed the browser, relog server and it returned to normal
Want results from more Discord servers?
Add your server