Skip to content

[BUG]: PostgreSQL: describeReferences() gives the wrong column pairs for a composite foreign key #17680

Closed

Description

@niden

Describe the bug
On PostgreSQL, describeReferences() gives the wrong columns for a foreign key with two or more columns. Each column of the key is paired with each referenced column, so a key with two columns gives four columns. A key with one column is correct. MySQL and MariaDB are correct.

To Reproduce

CREATE TABLE lines (
    order_id INTEGER NOT NULL,
    number   INTEGER NOT NULL,
    PRIMARY KEY (order_id, number)
);

CREATE TABLE shipments (
    id       SERIAL PRIMARY KEY,
    order_id INTEGER NOT NULL,
    line_no  INTEGER NOT NULL,
    CONSTRAINT shipments_lines_fk FOREIGN KEY (order_id, line_no)
        REFERENCES lines (order_id, number)
);
$reference = $connection->describeReferences('shipments')['shipments_lines_fk'];

var_dump($reference->getColumns(), $reference->getReferencedColumns());

Expected behavior

  • Columns: ['order_id', 'line_no']
  • Referenced columns: ['order_id', 'number']

Actual behavior

  • Columns: ['line_no', 'line_no', 'order_id', 'order_id']
  • Referenced columns: ['number', 'order_id', 'number', 'order_id']

Cause
Phalcon\Db\Dialect\Postgresql::describeReferences() joins key_column_usage and constraint_column_usage only on the constraint name. constraint_column_usage does not give the position of the columns. The query must pair the columns by position, with one of these methods: - Join key_column_usage through referential_constraints to the referenced unique constraint, with
position_in_unique_constraint = ordinal_position.

  • Read pg_constraint and use unnest(conkey, confkey) WITH ORDINALITY.

Details

  • Phalcon: cphalcon [5.x.x] and phalcon/phalcon [x.x.x] (the same bug is in both)
  • PHP: 8.1
  • PostgreSQL: 18

Activity

  1. self-assigned this
    on Oct 16, 2026
  2. added
    bug
    on Oct 16, 2026
  3. added theissue type on Oct 16, 2026
  4. added
    5.0
    on Oct 16, 2026
  5. moved this to Active Sprint in Phalcon v5on Oct 16, 2026
  6. niden commented on Oct 17, 2026

    @niden
    SponsorMemberAuthor

    Resolved in #17681

  7. moved this from Active Sprint to Implemented in Phalcon v5on Oct 17, 2026
  8. moved this from Implemented to Released in Phalcon v5on Nov 29, 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

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions