Skip to content

[BUG]: PostgreSQL describeReferences() gives the database name as the referenced schema #17816

Done

Description

@niden

Describe the bug
The query of Phalcon\Db\Dialect\Postgresql::describeReferences() gives current_database() AS REFERENCED_TABLE_SCHEMA. That is the name of the database, not the schema of the referenced table. So Reference::getReferencedSchema() is wrong for each foreign key on PostgreSQL. A key to a table in another schema cannot be followed.

To Reproduce

PostgreSQL, a database shop:

CREATE SCHEMA billing;
CREATE TABLE public.customers (id int PRIMARY KEY);
CREATE TABLE billing.invoices (
    id          int PRIMARY KEY,
    customer_id int REFERENCES public.customers (id)
);
CREATE TABLE billing.lines (
    id         int PRIMARY KEY,
    invoice_id int REFERENCES billing.invoices (id)
);
$connection->describeReferences('invoices', 'billing')[...]->getReferencedSchema(); // 'shop'
$connection->describeReferences('lines', 'billing')[...]->getReferencedSchema();    // 'shop'

Expected behavior

  • billing.invoices: the referenced schema is public.
  • billing.lines: the referenced schema is billing.

The fix: join the namespace of the referenced table, and give its name:

JOIN pg_catalog.pg_namespace AS fn ON fn.oid = fc.relnamespace
...
fn.nspname AS REFERENCED_TABLE_SCHEMA

The query already joins pg_class AS fc ON fc.oid = con.confrelid (the referenced table) since #17680.

Details

  • Phalcon version: 5.0.x (927c773be7): phalcon/Db/Dialect/Postgresql.zep, describeReferences() (line 419; the column on line 436).
  • phalcon/phalcon v6.0.x (aeca48611) has the same query: src/Db/Dialect/Postgresql.php, describeReferences() (line 496; the column on line 515).
  • Tests: tests/database/Db/Dialect/DescribeReferencesTest.php (the SQL text) and tests/database/Db/Adapter/Pdo/DescribeReferencesPostgresqlTest.php.

Additional context
Found in phalcon/crest#86 (a schema for each table): crest make:model cannot write a relation for a foreign key to a table in another schema, because the referenced schema is wrong.

Activity

  1. self-assigned this
    on Nov 27, 2026
  2. added
    bug
    5.0
    on Nov 27, 2026
  3. added theissue type on Nov 27, 2026
  4. niden commented on Nov 27, 2026

    @niden
    SponsorMemberAuthor

    Resolved in #17820

  5. added 2 commits that reference this issue on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

5.0bugstatus: medium

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions