© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
3 replies
shot

query with one to many (postgres + foreign keys)

I have the following schema:
export const accounts = pgTable("accounts", {
  id: serial("id").primaryKey(),
  address: text("address").notNull(),
  createdAt: timestamp("created_at").defaultNow().notNull(),
  updatedAt: timestamp("updated_at").defaultNow(),
  points: integer("points").default(0).notNull(),
});

export const nfts = pgTable("nfts", {
    id: serial("id").primaryKey(),
    contractAddress: text("contract_address").notNull(),
    tokenId: integer("token_id").notNull(),
    accountId: integer("account_id").references(() => accounts.id)
});
export const accounts = pgTable("accounts", {
  id: serial("id").primaryKey(),
  address: text("address").notNull(),
  createdAt: timestamp("created_at").defaultNow().notNull(),
  updatedAt: timestamp("updated_at").defaultNow(),
  points: integer("points").default(0).notNull(),
});

export const nfts = pgTable("nfts", {
    id: serial("id").primaryKey(),
    contractAddress: text("contract_address").notNull(),
    tokenId: integer("token_id").notNull(),
    accountId: integer("account_id").references(() => accounts.id)
});


I am trying to query for a specific accounts and get all associated nfts with:
db.query.accounts.findFirst({
  where: eq(accounts.address, "0xDEAD...BEEF"),
  with: { nfts: true },
});
db.query.accounts.findFirst({
  where: eq(accounts.address, "0xDEAD...BEEF"),
  with: { nfts: true },
});

This results in the error:
Invalid drizzle query
Invalid drizzle query
.

Do I have to use drizzles
relations
relations
to associate nfts to accounts instead of just foreign keys in order to use
db.query
db.query
?
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

one to many query
Drizzle TeamDTDrizzle Team / help
3y ago
Many to Many Query With
Drizzle TeamDTDrizzle Team / help
17mo ago
circular foreign keys
Drizzle TeamDTDrizzle Team / help
3y ago
Query only one item from Many-To-Many
Drizzle TeamDTDrizzle Team / help
3y ago