Skip to content

[BUG]: SQLite describeReferences() gives null referenced columns for a key with no referenced columns #17815

Done

Description

@niden

Describe the bug
SQLite accepts a foreign key with no referenced columns, for example REFERENCES users. The key then points to the primary key of users. PRAGMA foreign_key_list gives null in column 4 (to) for such a key.

Phalcon\Db\Adapter\Pdo\Sqlite::describeReferences() copies that null into referencedColumns. The reference then has no referenced column.

To Reproduce

SQLite 3.46.1:

CREATE TABLE users (id INTEGER PRIMARY KEY);
CREATE TABLE customers (a INT, b INT, PRIMARY KEY (a, b));
CREATE TABLE orders (
    id   INTEGER PRIMARY KEY,
    cust INT REFERENCES users
);
CREATE TABLE notes (
    x INT,
    y INT,
    FOREIGN KEY (x, y) REFERENCES customers
);

PRAGMA foreign_key_list('orders') gives (0, 0, 'users', 'cust', None, ...).
PRAGMA foreign_key_list('notes') gives (0, 0, 'customers', 'x', None, ...) and (0, 1, 'customers', 'y', None, ...).

$references = $connection->describeReferences('orders');
$references['foreign_key_0']->getReferencedColumns(); // [null]

Expected behavior

The fix: when to is null, use the primary key of the referenced table. PRAGMA table_info('<referenced table>') gives the primary key columns: the rows with pk > 0, in the order of pk. Match them to the columns of the key by seq.

Details

Additional context
Found in phalcon/crest#87: crest make:model cannot write a relation for such a key. It skips it with the reason the key does not name the referenced columns.

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 #17819

  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