Comments (4)
Hello @schveiguy
It's not documented in the README, but you can pass a dictionary to the key
argument of fetchOne
:
MyRecord.fetchOne(db, key: ["col1": val1, "col2": val2])
Documentation is missing, but the feature is there to stay: I want GRDB to support any primary key.
This variant of fetchOne
actually supports any key, not only primary keys, because it does not introspect the database schema. It implicitly requires that the dictionary columns are actually a table key, and returns the first record that matches.
Why did I not add any generic function that eats a tuple of 2, 3, ... N items? Because these functions would depend on the ordering of columns in the primary key. And this ordering is not always obvious, or may even be fragile (in case of wild schema change):
// Not supported, on purpose:
MyRecord.fetchOne(db, key: (1, 2)) // what is 1? what is 2?
So now, I suggest you write your own extension, as below :-)
extension MyRecord {
static func fetchOne(db: Database, key: (Int64, Int)) -> MyRecord? {
return fetchOne(db, ["col1": key.0, "col2": key.1])
}
}
from grdb.swift.
Hm... yes, that's much better than my SQL string forward that I had written, thanks :)
from grdb.swift.
You're welcome 😄
from grdb.swift.
I added a paragraph in README.md: https://github.com/groue/GRDB.swift#fetching-by-primary-key
from grdb.swift.
Related Issues (20)
- Confusion over .including(all:) query HOT 6
- How can I do floating-point division on an association count? HOT 8
- Latest version 6.26.0 is not deployed to the Cocoapod HOT 4
- Please add the Privacy Manifest file to the v5.x release. HOT 1
- package 'package.swift' is using Swift tools version 3.1.0 which is no longer supported; consider using '/ swift-tools-version: 5.9' to specify the current tools... HOT 1
- Request: Add citation to the repo HOT 3
- Cocoapods hasn't published since 6.24.1 HOT 2
- Cocoapods 6.26.0 release is not available in Cocapods Specs repo HOT 1
- Privacy Manifest: Entry missing for accessing file timestamps via creationDate API HOT 4
- When transaction updating multiple records in the database, if one of the records does not exist, it leads to update failure. HOT 4
- Avoid conflict with other SPM packages by renaming the CSQLite SPM target
- ValueObservation.tracking called on the updating thread HOT 5
- GRDB does not seem to copy over all rows when inserting within a migration HOT 4
- Unable to decode database NULL value as Swift nil value of type Int? when using ScopeAdapter built from splittingRowAdapters HOT 13
- PlayerRequest.swift is identical in GRDBAsyncDemo and GRDBCombineDemo HOT 4
- App crashes with many (not all) users running iOS 17.4.1 HOT 8
- Library fails to compile when targeting Linux using a SQLite library that doesn't support snapshots HOT 3
- 0xdead10cc prevention — `checkForSuspensionViolation` on every `Statement.step` HOT 6
- Weird db read issue HOT 7
- Error for query count grouped HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grdb.swift.