Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-warehouse): report no external schema for an unsynced table (#101091)

Daniel RCbde4a4221062
diff --git a/products/customer_analytics/frontend/scenes/CustomerAnalyticsConfigurationScene/account/customPropertyDefinitionsLogic.test.ts b/products/customer_analytics/frontend/scenes/CustomerAnalyticsConfigurationScene/account/customPropertyDefinitionsLogic.test.ts
index 79f0646206e0..dd0290dd8fdb 100644
--- a/products/customer_analytics/frontend/scenes/CustomerAnalyticsConfigurationScene/account/customPropertyDefinitionsLogic.test.ts
+++ b/products/customer_analytics/frontend/scenes/CustomerAnalyticsConfigurationScene/account/customPropertyDefinitionsLogic.test.ts
@@ -571,6 +571,32 @@ describe('customPropertyDefinitionsLogic', () => {
expect(logic.values.hasWarehouseSourceOptions).toBe(true)
})
+ it('never offers a table whose schema carries no id', async () => {
+ useMocks({
+ ...defaultMocks(),
+ get: {
+ ...defaultMocks().get,
+ [WAREHOUSE_TABLES_URL]: {
+ count: 2,
+ results: [
+ buildTable(),
+ // An unsynced table: the API can report a schema object with no id, and binding
+ // needs the id, so offering this gives the user a pick that fails at save.
+ buildTable({ id: 'table-2', name: 'Intercom_split', external_schema: { name: '' } }),
+ ],
+ },
+ },
+ })
+ mountLogic()
+ await expectLogic(logic, () => logic.actions.openCreateModal()).toDispatchActions([
+ 'loadWarehouseTablesSuccess',
+ ])
+ expect(logic.values.warehouseSourceOptions).toEqual([
+ { value: 'table:table-1', label: 'users', kind: 'table' },
+ { value: 'view:view-1', label: 'billing_view', kind: 'view' },
+ ])
+ })
+
it("loads a view's columns from the saved query rather than fetching a table", async () => {
useMocks(defaultMocks())
mountLogic()
diff --git a/products/customer_analytics/frontend/scenes/CustomerAnalyticsConfigurationScene/account/customPropertyDefinitionsLogic.ts b/products/customer_analytics/frontend/scenes/CustomerAnalyticsConfigurationScene/account/customPropertyDefinitionsLogic.ts
index fc4494c25193..6a2eeadf228d 100644
--- a/products/customer_analytics/frontend/scenes/CustomerAnalyticsConfigurationScene/account/customPropertyDefinitionsLogic.ts
+++ b/products/customer_analytics/frontend/scenes/CustomerAnalyticsConfigurationScene/account/customPropertyDefinitionsLogic.ts
@@ -808,14 +808,20 @@ export const customPropertyDefinitionsLogic = kea<customPropertyDefinitionsLogic
break
}
}
- // Only synced tables carry an external_schema, which is what a table binding needs.
- const synced = collected.filter((table) => !!table.external_schema)
+ // A table binds by its schema id, so require the id rather than the object: an
+ // unsynced table can still carry an id-less schema, and offering it gives the user
+ // a pick that fails at save.
+ const synced = collected.filter((table) => !!table.external_schema?.id)
// Keep the currently-selected table in the list even if the active search filters it
// out, so the picker can still render its label rather than a bare id.
const selected = decodeWarehouseSource(values.customPropertyForm.warehouseSource)
const selectedId = selected?.kind === 'table' ? selected.id : null
if (selectedId && !synced.some((table) => table.id === selectedId)) {
- const known = values.warehouseTables.find((table) => table.id === selectedId)
+ // Same invariant as the filter above, so the two cannot drift: a restored table
+ // is only offered when it still carries a schema id to bind by.
+ const known = values.warehouseTables.find(
+ (table) => table.id === selectedId && !!table.external_schema?.id
+ )
if (known) {
return [known, ...synced]
}
diff --git a/products/data_warehouse/backend/presentation/views/table.py b/products/data_warehouse/backend/presentation/views/table.py
index c84f2d6db11c..a9fd357900cc 100644
--- a/products/data_warehouse/backend/presentation/views/table.py
+++ b/products/data_warehouse/backend/presentation/views/table.py
@@ -279,7 +279,13 @@ class TableSerializer(UserAccessControlSerializerMixin, serializers.ModelSeriali
SimpleExternalDataSchemaSerializer,
)
- return SimpleExternalDataSchemaSerializer(instance.externaldataschema_set.first(), read_only=True).data or None
+ schema = instance.externaldataschema_set.first()
+ if schema is None:
+ # `Serializer(None).data` is not falsy: DRF falls back to `get_initial()`, which returns a
+ # dict of the serializer's writable fields. Trusting `or None` emitted an id-less schema
+ # object for every unsynced table, which callers read as "this table has a schema".
+ return None
+ return SimpleExternalDataSchemaSerializer(schema, read_only=True).data
def create(self, validated_data):
team_id = self.context["team_id"]
diff --git a/products/data_warehouse/backend/tests/api/test_table.py b/products/data_warehouse/backend/tests/api/test_table.py
index d5f6a5b73641..56b4056f3ca4 100644
--- a/products/data_warehouse/backend/tests/api/test_table.py
+++ b/products/data_warehouse/backend/tests/api/test_table.py
@@ -528,6 +528,19 @@ class TestTable(APIBaseTest):
assert SimpleTableSerializer().get_hogql_name(table) == "self_managed_table"
+ def test_table_without_a_schema_reports_no_external_schema(self):
+ DataWarehouseTable.objects.create(
+ name="unsynced_table", format="Parquet", team=self.team, team_id=self.team.pk, columns={}
+ )
+
+ response = self.client.get(f"/api/environments/{self.team.id}/warehouse_tables/")
+
+ assert response.status_code == 200
+ table = next(row for row in response.json()["results"] if row["name"] == "unsynced_table")
+ # An id-less object here reads as "this table has a schema" to every consumer, and anything
+ # that then binds by `external_schema.id` gets nothing.
+ assert table["external_schema"] is None
+
def test_refresh_schema_direct_postgres_table_not_exposed_via_warehouse_tables_api(self):
source = ExternalDataSource.objects.create(
team=self.team,