Repository navigation
Description
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:catocustomers.aforeign_key_1:cbtocustomers.bforeign_key_2:user_idtousers.id
Expected behavior
Two references:
foreign_key_0: the columns['ca', 'cb']tocustomers['a', 'b']foreign_key_1:['user_id']tousers['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
nullintoreferencedColumnsfor 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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsReleased
Activity
niden commentedon Nov 27, 2026
Resolved in #17818
[phalcon#17814] - giving one reference for each composite foreign key…
[phalcon#17814] - updating changelog and ext