HubDB Filters and Foreign IDs: Query and Join Tables

Published on 10 min read
HubDB filters foreign ID

HubDB is the moment a HubSpot site stops being a set of pages and becomes a small application. A team directory, a product catalog, a locations finder, a resource library: each is a table, and each page is a query against it. The first version is always easy, a loop over all the rows. The trouble starts when the data grows, when one table needs to reference another, and when a page quietly makes its eleventh query and stops rendering. HubDB has rules, and the sites that scale are the ones built with the rules in mind.

In this guide, I will cover the two things that trip up most HubDB projects: filtering, with the query syntax, the ordering, the limits and the pattern that avoids repeated calls, and joining, with the Foreign ID column that lets one table reference rows in another and the nested loop that renders the joined data. Along the way, the technical limits that decide how you structure the tables in the first place. If HubDB is new to you, it helps to see first where it sits among themes, modules and server-side logic in Content Hub development as a whole.

Part of the guide: HubSpot implementation: the guide and checklist

Where HubDB lives and who can use it

A HubDB table is rows, columns and cells, like a spreadsheet, with a globally unique id for the table, each row and each column. Rows come with built-in columns: hs_id, hs_created_at, and for dynamic pages hs_path and hs_name. Column types cover text, rich text, URL, image, select, multi-select, date, date and time, number, currency, checkbox, location, video, and the one this guide is about, Foreign ID.

Two things to know before designing. Using HubDB data in pages requires Content Hub Professional or Enterprise, and programmable emails that read HubDB require Marketing Hub Enterprise. And a new table starts as a draft: it cannot be read through HubL or the API until it is published, which explains the classic confusion where data shows in the editor preview but not on the live page. If the live page is missing rows, check whether the table was published after they were added.

Querying rows with hubdb_table_rows

The HubL function hubdb_table_rows returns a table's rows, and takes the table id or name plus a filter query string. Reference tables by name rather than id, because names travel across portals when you move a theme and ids do not. The filter query uses the same syntax as the HubDB HTTP API: the column name, a double underscore, an operator, and a value, chained with ampersands, plus orderBy for sorting.

    employees__gt=10&orderBy=count category__contains=automation orderBy=-published_date&limit=1500

Two defaults matter. The function returns at most one thousand rows unless you pass a higher limit, and a table holds at most ten thousand rows, so for large tables plan the query rather than assuming you will get everything. Column names are case insensitive, and you can read a value as row.column_name or row["column name"]. To fetch a single known row, use hubdb_table_row with the row id instead of filtering.

The ten-call limit and the pattern that avoids it

The rule that breaks the most pages is this one: a CMS page can make at most ten calls to hubdb_table_rows. A template that queries once per section, or once per category in a loop, hits that ceiling fast and the page stops rendering. The fix is the pattern HubSpot itself recommends: make one query that fetches the rows you need, then filter in HubL with selectattr and rejectattr instead of querying again.

   

One query and several in-memory filters is cheaper, faster and immune to the limit. Save the query result in a variable at the top of the template and derive every view of it from that variable. If you find yourself writing a second hubdb_table_rows for the same table, it is almost always a sign the first query should have fetched more and the second should have been a selectattr.

Foreign ID: joining one table to another

Real data is relational, and HubDB handles that with the Foreign ID column type. A Foreign ID column on a table points at another table, so a row in a team table can reference rows in an expertise table, or a product can reference rows in a category table. When you add the column you choose the foreign table and a display column, which is the value editors see when picking related rows in the HubDB interface. That display choice is cosmetic: all the foreign table's columns are available when you render.

In the editor, a Foreign ID column behaves like a multi-select of the foreign table's rows, so one row can reference several. In HubL, the column holds a list of the related rows, and you render them with a nested loop: the outer loop over the main table, the inner loop over the Foreign ID column of each row, reading any column of the related row. It is the same for loop you already use in HubL for repeater fields, only now the list comes from a table and not from a module field.

This is how you keep shared data in one place. Instead of typing the same category name into a hundred product rows, you keep categories in their own table and reference them, so renaming a category is one edit and every product follows. The join is read-time: when you render, the related rows come along with the main rows, with no extra query and no hit on the ten-call limit.

What Foreign ID does not do

Foreign ID is a join, not a query language, and knowing its edges prevents the most common frustrations. The order in which related rows were picked is not something you can rely on for display; if a specific order matters, sort in HubL or model the data differently. Filtering the main table by a value inside the foreign table is not a first-class filterQuery operation; fetch the rows and filter in HubL, or carry the value you filter on as a column on the main table. And selectattr on a Foreign ID column compares the list, not a field inside it, so filter on a related row's field with a loop or by flattening the data first.

Practitioners hit all three and conclude HubDB is limited. It is more accurate to say HubDB is a document store with a join, not a relational database. Design the tables so the columns you filter and sort by live on the table you query, and use Foreign ID for what it is good at: pulling related, shared data along with a row at render time.

A tip from someone who has been burned: count your hubdb_table_rows calls per page before you ship, including the ones hidden inside modules and partials that the template includes. A module that queries HubDB and is placed five times on a page spends five of your ten calls before the template has done anything. Query once at the top, pass the data down, and let modules receive rows instead of fetching them.

Dynamic pages and the paths they need

When a table drives dynamic pages, each row becomes a page, with hs_path as the last segment of the URL and hs_name as the page title. Two rules apply. Paths must be lowercase, so that the URLs are case insensitive. And at most ten dynamic pages can use the same HubDB table, so a large site organizes its dynamic content across tables rather than piling every dynamic page onto one. Row attribute hs_child_table_id is how a multilevel dynamic setup links a row to the table that populates its children.

Why this matters for your operation

A well-structured HubDB turns content that used to be a developer ticket into content marketing manages in a table: add a row, a page appears; change a category, every product updates. That autonomy is the whole value of data-driven pages, and it depends on tables designed around the query patterns and the limits rather than against them.

A poorly structured one produces the opposite experience: pages that stop rendering at the eleventh query, categories duplicated across hundreds of rows, and joins nobody can filter by. The rules in this guide, one query and in-memory filters, Foreign ID for shared data, filterable columns on the table you query, lowercase paths and published tables, are what make HubDB the small application it is meant to be instead of a spreadsheet that breaks under its own weight.

In practice: the directory that stopped rendering

A company built a team directory with a module that queried HubDB for members of a given department, and marketing placed that module once per department on the page. At eight departments the page worked. When they added a ninth and a tenth, and the template itself made two more queries for featured members, the page crossed ten calls and stopped rendering, with no change to any code.

The fix was the one-query pattern. The template fetched all members once at the top and passed the rows into the module, which filtered its department with selectattr instead of querying. The page went from twelve calls to one, rendered faster, and marketing could add departments without a developer. The data never changed. How the page asked for it did.

Checklist for HubDB queries and joins

  • Is the table published, so rows appear on the live page and not only in the preview?
  • Do you reference tables by name rather than id, so the theme ports across portals?
  • Does each page make at most a few hubdb_table_rows calls, with selectattr and rejectattr doing the rest?
  • Is shared data kept in its own table and referenced through Foreign ID columns?
  • Do the columns you filter and sort by live on the table you query?
  • For dynamic pages, are paths lowercase and is no table driving more than ten dynamic pages?
  • Did you set a limit when you need more than the default thousand rows?

Frequently asked questions

How do I filter rows in hubdb_table_rows?

With a filter query string using the HTTP API syntax: the column name, a double underscore and an operator, such as department__eq=sales or employees__gt=10, chained with ampersands and combined with orderBy for sorting.

Why did my page stop rendering after I added a section?

Most likely you crossed the limit of ten hubdb_table_rows calls per page, counting those inside modules. Query once at the top of the template and filter the result in HubL with selectattr instead.

What is a Foreign ID column?

A column that references rows in another HubDB table, letting you join tables. Editors pick related rows as a multi-select, and in HubL you render them with a nested loop, with every column of the related row available.

Can I filter the main table by a value in the foreign table?

Not as a first-class filter query. Fetch the rows and filter in HubL with a loop, or carry the value you filter on as a column on the main table so it can be queried directly.

Why does data show in the preview but not on the live page?

Because the table has not been published since the data was added. Draft table data appears in the editor and previews; only published data appears on the live page.

How many rows does hubdb_table_rows return?

At most one thousand by default. Pass a higher limit in the filter query to retrieve more, up to the ten thousand rows a table can hold.

Let's talk? Building catalogs, directories or dynamic pages on HubDB and want them to scale without hitting the limits? At Insight Sales we design the tables and the templates together. Talk to our team and find out how we can help.

Talk to a specialist

What's next for your revenue operation? Let's look at it together.

A quick conversation with an engineer. (Not a sales call.)

Tags

Renato Ferreira - Founder

About the author

Entrepreneur with 15+ years in Sales & Marketing. Founded 4 companies (2 exits). Pioneer of Outbound Marketing in Brazil. Co-Founder at Insight Sales.