Skip to content

Select ids with selectID() #1719

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

Merged
merged 3 commits into from
Feb 12, 2025
Merged

Select ids with selectID() #1719

merged 3 commits into from
Feb 12, 2025

Conversation

groue
Copy link
Owner

@groue groue commented Feb 11, 2025

For Identifiable record types, the new selectID method is easier to use than selectPrimaryKey(as:):

struct Player: Identifiable, TableRecord {
    var id: Int64
}

// Before
let ids = try Player.selectPrimaryKey(as: Int64.self).fetchSet(db)

// After
let ids = try Player.selectID().fetchSet(db)

This pull request reverts 7ffc332.

At the time, selectID was replaced with selectPrimaryKey(as:) because:

Since Optional is a database value like others, selectID would be a request of optionals for records with an optional ID type:

struct Player: Identifiable, MutablePersistableRecord {
    var id: Int64?
}

Player.selectID() // QueryInterface<Int64?>

This is not good, because primary keys are not null. And those requests would fetch impractical types such as [Int64?], Set<Int64?>, Int64??.

This problem is reintroduced, but we now recommend against Identifiable types with optional ids in Recommended Practices for Designing Record Types.

Records with optional ids can use selectPrimaryKey(as:) in order to remove the undesired optional:

// NOT RECOMMENDED: Identifiable type with optional ID 
struct Player: Identifiable, MutablePersistableRecord {
    var id: Int64? 
}

// NOT RECOMMENDED: QueryInterface<Int64?>
Player.selectID() 

// OK: QueryInterface<Int64>
Player.selectPrimaryKey(as: Int64.self) 

This reverts 7ffc332

At the time, selectID was replaced with selectPrimaryKey(as:) because:

> Since Optional is a database value like others, selectID would be a request of optionals for records with an optional ID type:
>
>     struct Player: Identifiable, MutablePersistableRecord {
>         var id: Int64?
>     }
>
>     Player.selectID() // QueryInterface<Int64?>
>
> This is not good, because primary keys are not null. And those requests would fetch impractical types such as [Int64?], Set<Int64?>, Int64??.

This problem is reintroduced, but we now recommend against optional ids in https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/recordrecommendedpractices. And record with optional ids can use selectPrimaryKey(as:).
@groue groue changed the title selectID Select ids with selectID() Feb 12, 2025
@groue groue merged commit 54ed7ec into development Feb 12, 2025
8 checks passed
@groue groue deleted the dev/selectID branch February 12, 2025 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant