{"record":{"id":"16589c0adfb5efe1","repo":"pathwaycom/pathway","slug":"primary-key-references-column-s-foreign-that-ar","errorCode":null,"errorMessage":"primary_key references column(s) {foreign} that are not present in the written table; pass ColumnReferences from the table being written, e.g. primary_key=[table.k].","messagePattern":"primary_key references column\\(s\\) (.+?) that are not present in the written table; pass ColumnReferences from the table being written, e\\.g\\. primary_key=\\[table\\.k\\]\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/pathway/io/postgres/__init__.py","lineNumber":918,"sourceCode":"    if (\n        _external_diff_column is not None\n        and _external_diff_column._column.dtype != dtype.INT\n    ):\n        raise ValueError(\"_external_diff_column can only have an integer type\")\n\n    external_diff_column_index = get_column_index(table, _external_diff_column)\n    key_field_names = None\n    if primary_key is not None:\n        key_field_names = [pkey_field.name for pkey_field in primary_key]\n        # `primary_key=[other_table.col]` (or a reference whose name\n        # simply isn't in `table`) is accepted today but then either\n        # generates a malformed CREATE TABLE (``PRIMARY KEY\n        # (\"unknown\")``) on init or produces an UPSERT that panics at\n        # flush. Reject up-front with a clear message.\n        table_columns = set(table.schema.column_names())\n        foreign = sorted(n for n in key_field_names if n not in table_columns)\n        if foreign:\n            raise ValueError(\n                f\"primary_key references column(s) {foreign} that are \"\n                \"not present in the written table; pass ColumnReferences \"\n                \"from the table being written, e.g. primary_key=[table.k].\"\n            )\n        # A duplicate reference like `primary_key=[t.k, t.k]` would slip\n        # through to `SqlQueryTemplate` and either yield malformed SQL\n        # (``PRIMARY KEY (\"k\", \"k\")``) on CREATE TABLE or corrupt the\n        # ``primary_key_fields`` index-swap used on DELETE. Reject it\n        # here with a clear message instead.\n        duplicates = sorted(\n            {name for name in key_field_names if key_field_names.count(name) > 1}\n        )\n        if duplicates:\n            raise ValueError(f\"primary_key contains duplicate column(s) {duplicates}\")\n        # A nullable primary-key column in snapshot mode is silently\n        # broken: either the `NOT NULL` PRIMARY KEY we emit on\n        # create_if_not_exists / replace rejects the NULL row at\n        # insert time, or (against a pre-existing table that allows","sourceCodeStart":900,"sourceCodeEnd":936,"githubUrl":"https://github.com/pathwaycom/pathway/blob/fa2f74a4649b7c5908690cf60137263d8d80de5f/python/pathway/io/postgres/__init__.py#L900-L936","documentation":"Raised by pw.io.postgres.write() in snapshot mode when one or more names in primary_key do not exist among the written table's columns. Without the check the engine would emit malformed SQL such as PRIMARY KEY (\"unknown\") during CREATE TABLE, or an UPSERT that panics at flush — after init_mode may already have modified the destination.","triggerScenarios":"primary_key=[other_table.k] where the reference's name is not a column of the table being written; primary_key=[t.col] after renaming that column upstream; passing a plain string that names nothing in the schema.","commonSituations":"Copy-pasting a key reference from another table variable; joins/rename operations that changed column names between drafting the call and running it.","solutions":["Pass references from the table being written: primary_key=[table.k].","After renames, update the key reference to the new column name.","If keying on another table's column, select/join it into this table first so the name exists here."],"exampleFix":"# before\npw.io.postgres.write(t, parts, \"tbl\", output_table_type=\"snapshot\", primary_key=[other.id])\n# after\njoined = t.join(other, t.id == other.id).select(t.id, t.value, other.extra)\npw.io.postgres.write(joined, parts, \"tbl\", output_table_type=\"snapshot\", primary_key=[joined.id])","handlingStrategy":"validation","validationCode":"table_cols = set(table.schema.column_names())\nforeign = [c.name for c in (primary_key or []) if c.name not in table_cols]\nassert not foreign, f\"primary_key references foreign columns: {foreign}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always build primary_key from the same table object passed as the first write() argument.","After renames/joins, re-derive key references from the final table.","Add a helper that resolves keys by name against table.schema.column_names() at pipeline-build time."],"tags":["postgres","pathway","snapshot","primary-key","column-reference"],"backgroundTag":null,"analyzedSha":"fa2f74a4649b7c5908690cf60137263d8d80de5f","analyzedAt":"2026-08-15T01:48:17.006Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}