Skip to content

SchemaReader column definitions are incomplete for composite primary keys #1216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stefansaasen opened this issue Jun 7, 2023 · 4 comments
Closed

Comments

@stefansaasen
Copy link
Contributor

Given the following CREATE TABLE statement:

CREATE TABLE t (
    col1 INTEGER,
    col2 INTEGER,
    col3 INTEGER,
    PRIMARY KEY (col1, col2)
);

A call to db.schema.columnDefinitions(table: "t") should return three ColumnDefinitions where the column definitions for col1 and col2 are marked as being part of the primary key.

Instead, only the first column is marked as being part of the primary key.

Example test case (e.g. in SchemaReaderTests):

    func test_columnDefinitions_composite_primary_keys() throws {
        try db.run("""
        CREATE TABLE t (
          col1 INTEGER,
          col2 INTEGER,
          col3 INTEGER,
          PRIMARY KEY (col1, col2)
        );
        """)

        XCTAssertEqual(
            try schemaReader.columnDefinitions(table: "t"), [
            ColumnDefinition(
                    name: "col1",
                    primaryKey: .init(autoIncrement: false),
                    type: .INTEGER,
                    nullable: true,
                    defaultValue: .NULL,
                    references: nil),
            ColumnDefinition(
                    name: "col2",
                    primaryKey: .init(autoIncrement: false),
                    type: .INTEGER,
                    nullable: true,
                    defaultValue: .NULL,
                    references: nil),
            ColumnDefinition(
                    name: "col3",
                    primaryKey: nil,
                    type: .INTEGER,
                    nullable: true,
                    defaultValue: .NULL,
                    references: nil)
            ]
        )
    }

The test fails, as the second column is not detected as being part of the composite primary key.

This is due to the following line:

primaryKey: row[TableInfoTable.primaryKeyColumn] == 1 ?

The PRAGMA table_info that is used to return the column definitions, returns one row for each defined column. The pk column contains:

... either zero for columns that are not part of the primary key, or the 1-based index of the column within the primary key).

See https://www.sqlite.org/pragma.html#pragma_table_info

A possible fix could be to change that line to:

    primaryKey: (row[TableInfoTable.primaryKeyColumn] ?? 0) > 0 ?

Build Information

  • Include the SQLite.swift version: 0.14.1
  • Mention Xcode and OS X versions affected: Xcode 14.3 and macOS Ventura 13.4
  • How do do you integrate SQLite.swift in your project: Swift Package manager
@stefansaasen
Copy link
Contributor Author

See #1217 for a possible fix.

@stefansaasen
Copy link
Contributor Author

#1217 was merged to master, so I guess this can be closed.

cc @nathanfallet

@nathanfallet
Copy link
Collaborator

You're right. I didn't see your PR was not marking this issue for auto fix (using "Fix #..." in the PR description)

@stefansaasen
Copy link
Contributor Author

Good point. I've created another issue and a possible fix and linked them in both directions now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants