I’m having trouble accessing data from connected Notion databases. When I query my database that has relation properties linking to other databases, the related information doesn’t come through properly. The relation columns appear empty even though they should contain references to other database entries.
Here’s what I’m currently doing:
database = notion_client.get_database(database_id)
entries = [entry for entry in database.collection.get_entries()]
related_data = getattr(entry, relation_property_name)
Is there a specific approach or API method I should use to properly fetch the linked database content? I need to access the actual data from the related tables, not just the references.
notion’s relation handling is a bit tricky. try using retrieve with filter_properties to get all the linked data at once instead of looping through each entry. it saves you from hitting those pesky rate limits!
The issue you’re encountering is typical with Notion’s API, which returns page references rather than full property data from relation fields. Attempting to use getattr(entry, relation_property_name) won’t yield the desired results since these relations must be handled differently. First, retrieve your main entries as you are doing, but then you need to loop through the relationship IDs you find and call notion_client.pages.retrieve(page_id) for each ID separately. This approach allows you to fetch the actual data you need from the linked pages. Additionally, ensure your integration has the necessary permissions to access all related databases to avoid empty results.
This is a super common issue with Notion’s relation properties. The problem is that relations return page objects, not the actual property values you’re after. When you pull your main database entries, those relation fields are just page references that need different handling. Don’t use getattr(entry, relation_property_name) directly - it won’t work. Instead, loop through the returned page objects and query each page’s properties separately. The relation gives you an array of page objects with IDs, but to grab the actual content (titles, other properties), you’ve got to access each page object’s properties individually. Also make sure your integration token has access to both your source database AND all the linked databases - otherwise you’ll get empty results even with perfect code.
Yeah, Notion API relations are just reference IDs by default. You need to expand them when querying. Use the filter_properties parameter or stick with the official Notion API for proper pagination. When you query the database, include the relation property names in your request - the API will return the linked page objects with all their properties filled in. Still getting empty results? Check your integration has read permissions for both databases. Half the time it’s just missing access rights to the linked database, not your code.
yep, you gotta make individual calls for each related entry. the notion API just gives you those ID refs, not the actual info. get your main entries, then loop thru the relation IDs and use notion_client.pages.retrieve() for each. i know, it’s a pain!