Closed stefansaasen closed 4 months ago
Thanks for reporting this. Can you take a look at #1210 to see if this fixes your problem?
Thanks for looking into this! That does in fact solve the problem and matches the information returned by e.g. PRAGMA foreign_key_list('track');
.
Should this be documented somehow? E.g. as the caller I need to work out what the primary key of the referenced table is (depending on the use case).
There's a code comment // when null, use primary key
, maybe surface this?
Yes, good point, I'll add some documentation to the ForeignKey
struct
.
The following error occurs using the schema reader (specifically
columnDefinitions(table:)
) for a particular table (see below for the tables/foreign key definition that triggers this):https://github.com/stephencelis/SQLite.swift/blob/7a2e3cd27de56f6d396e84f63beefd0267b55ccb/Sources/SQLite/Typed/Query.swift#L1216
This is due to the fact that the
foreignKeys
function in theSchemaReader
assumes that theto
colum returned by runningPRAGMA foreign_key_list("table name")
contains a valid primary key column and is not null:https://github.com/stephencelis/SQLite.swift/blob/7a2e3cd27de56f6d396e84f63beefd0267b55ccb/Sources/SQLite/Schema/SchemaReader.swift#L94-L107
The error occurs in line
100
:row[ForeignKeyListTable.toColumn]
.That is not necessarily true though.
How to reproduce
Minimal example (see below for the necessary
Package.swift
definition):The table definition is taken from the last example on https://www.sqlite.org/foreignkeys.html#fk_indexes (Section 3 "Required and Suggested Database Indexes").
The result of the
PRAGMA
query is the following table:Here the
to
column is empty.The SQLite source for the foreign key contains the following bits (see https://github.com/sqlite/sqlite/blob/master/src/sqliteInt.h#L2500):
So it seems valid to omit the primary key column and SQLite will automatically use the primary key column of the referenced table. On the flip side that means,
to
can in fact be null. In this example, the referenced column would beartist. artistid
(the primary key of theartist
table).Build Information
SQLite.swift version:
0.14.1
(also tested onmaster
at 1b1eba0aa28046f43114ddc2805893acad647461) Xcode version:14.3
macOS:13.3.1
SQLite.swift is integrated via the SPM: