© 2026 Hedgehog Software, LLC

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

Convert PostgreSQL to Drizzle ORM

I have the following SQL that I am trying to convert to Drizzle ORM for my NextJS project.

SELECT
  ffxiv_blue_mage_tracker_spells.id,
  ffxiv_blue_mage_tracker_spells.name,
  ffxiv_blue_mage_tracker_spells.description,
  ffxiv_blue_mage_tracker_spells.tooltip,
  ffxiv_blue_mage_tracker_spells.patch,
  ffxiv_blue_mage_tracker_spells.icon,
  ffxiv_blue_mage_tracker_spells.type,
  ffxiv_blue_mage_tracker_spells.aspect,
  (
    SELECT
      STRING_AGG(source_text, ', ')
    FROM
      ffxiv_blue_mage_tracker_sources AS src
    WHERE
      src.spell_id = ffxiv_blue_mage_tracker_spells.id
  ) AS where_to_aquire
FROM
  ffxiv_blue_mage_tracker_spells
  INNER JOIN (
    SELECT
      spell_id,
      -- Remove grouping by source_text
      STRING_AGG(source_text, ', ') AS source_text
    FROM
      ffxiv_blue_mage_tracker_sources
    GROUP BY
      spell_id
  ) AS ffxiv_blue_mage_tracker_sources ON ffxiv_blue_mage_tracker_spells.id = ffxiv_blue_mage_tracker_sources.spell_id
ORDER BY
  ffxiv_blue_mage_tracker_spells.id ASC
SELECT
  ffxiv_blue_mage_tracker_spells.id,
  ffxiv_blue_mage_tracker_spells.name,
  ffxiv_blue_mage_tracker_spells.description,
  ffxiv_blue_mage_tracker_spells.tooltip,
  ffxiv_blue_mage_tracker_spells.patch,
  ffxiv_blue_mage_tracker_spells.icon,
  ffxiv_blue_mage_tracker_spells.type,
  ffxiv_blue_mage_tracker_spells.aspect,
  (
    SELECT
      STRING_AGG(source_text, ', ')
    FROM
      ffxiv_blue_mage_tracker_sources AS src
    WHERE
      src.spell_id = ffxiv_blue_mage_tracker_spells.id
  ) AS where_to_aquire
FROM
  ffxiv_blue_mage_tracker_spells
  INNER JOIN (
    SELECT
      spell_id,
      -- Remove grouping by source_text
      STRING_AGG(source_text, ', ') AS source_text
    FROM
      ffxiv_blue_mage_tracker_sources
    GROUP BY
      spell_id
  ) AS ffxiv_blue_mage_tracker_sources ON ffxiv_blue_mage_tracker_spells.id = ffxiv_blue_mage_tracker_sources.spell_id
ORDER BY
  ffxiv_blue_mage_tracker_spells.id ASC


So far what I have is the following and I'm not entirely sure that what I have so far is even correct:
const spellList = await db
    .select({
      spellId: spells.id,
      name: spells.name,
      description: spells.description,
      tooltip: spells.tooltip,
      patch: spells.patch,
      icon: spells.icon,
      type: spells.type,
      aspect: spells.aspect,
      whereToAquire: sql<string>`select string_agg(${sources.sourceText}, ', ') from ${sources} as src where src.spell_id = ${spells.id}`,
    })
    .from(spells)
    .innerJoin(sources, eq(spells.id, sources.spellId))
    .orderBy(asc(spells.id));
const spellList = await db
    .select({
      spellId: spells.id,
      name: spells.name,
      description: spells.description,
      tooltip: spells.tooltip,
      patch: spells.patch,
      icon: spells.icon,
      type: spells.type,
      aspect: spells.aspect,
      whereToAquire: sql<string>`select string_agg(${sources.sourceText}, ', ') from ${sources} as src where src.spell_id = ${spells.id}`,
    })
    .from(spells)
    .innerJoin(sources, eq(spells.id, sources.spellId))
    .orderBy(asc(spells.id));


I am stuck on how to handle the INNER JOIN in my original SQL
Solution
That helped. Thank you
Jump to solution
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

🚨 Issue: Testing with PostgreSQL and Drizzle ORM using Testcontainers
Drizzle TeamDTDrizzle Team / help
14mo ago
Drizzle ORM issues
Drizzle TeamDTDrizzle Team / help
15mo ago
How does Drizzle ORM map PostgreSQL dates with regard to timezones?
Drizzle TeamDTDrizzle Team / help
2y ago
Issue with Drizzle ORM and drizzle-kit push with Tembo Cloud PostgreSQL
Drizzle TeamDTDrizzle Team / help
2y ago