Skip to content

[BUG]: SQLite describeReferences() gives one key for each column of a composite foreign key #17814

Done

Description

@niden

Describe the bug
Phalcon\Db\Adapter\Pdo\Sqlite::describeReferences() gives one reference for each column of a composite foreign key. A key with two columns gives two references, each with one column.

PRAGMA foreign_key_list gives one row for each column of a key. Column 0 (id) is the same for all the columns of one key. Column 1 (seq) is the position of the column in the key. The adapter does not use id: the name of the reference is foreign_key_<row number>, so each row becomes a new reference.

To Reproduce

SQLite 3.46.1:

CREATE TABLE customers (a INT, b INT, PRIMARY KEY (a, b));
CREATE TABLE users (id INTEGER PRIMARY KEY);
CREATE TABLE orders (
    id      INTEGER PRIMARY KEY,
    user_id INT REFERENCES users (id),
    ca      INT,
    cb      INT,
    FOREIGN KEY (ca, cb) REFERENCES customers (a, b)
);

PRAGMA foreign_key_list('orders') gives (id, seq, table, from, to, ...):

(0, 0, 'customers', 'ca', 'a', ...)
(0, 1, 'customers', 'cb', 'b', ...)
(1, 0, 'users', 'user_id', 'id', ...)
$references = $connection->describeReferences('orders');

Today the result has three references:

  • foreign_key_0: ca to customers.a
  • foreign_key_1: cb to customers.b
  • foreign_key_2: user_id to users.id

Expected behavior
Two references:

  • foreign_key_0: the columns ['ca', 'cb'] to customers ['a', 'b']
  • foreign_key_1: ['user_id'] to users ['id']

The fix: use id (column 0) for the name, foreign_key_<id>, and add the columns of each key in the order of seq (column 1).

Note: SQLite gives id in the reverse order of the declaration, and the rows are sorted by id. For a table with only single-column keys, the row number and id are the same, so the names do not change. For a table with a composite key, the names of the keys after it change (the number of rows goes down).

Details

  • Phalcon version: 5.0.x (927c773be7): phalcon/Db/Adapter/Pdo/Sqlite.zep, describeReferences() (line 419; the name on line 428).
  • phalcon/phalcon v6.0.x (aeca48611) has the same code: src/Db/Adapter/Pdo/Sqlite.php, describeReferences() (line 488; the name on line 501).
  • Tests: tests/database/Db/Adapter/Pdo/DescribeReferencesTest.php.
  • Related: the same method also puts null into referencedColumns for a key with no referenced columns: [BUG]: SQLite describeReferences() gives null referenced columns for a key with no referenced columns #17815.

Additional context
Found in phalcon/crest#87: crest make:model writes the relations from describeReferences(). On SQLite, each column of a composite key gives its own relation, and each relation uses only part of the key.

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. moved this from Backlog to Active Sprint in Phalcon v5on Nov 27, 2026
  5. niden commented on Nov 27, 2026

    @niden
    SponsorMemberAuthor

    Resolved in #17818

  6. moved this from Active Sprint to Implemented in Phalcon v5on Nov 27, 2026
  7. added 2 commits that reference this issue on Nov 28, 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