Queries against Unique keys (other than the id Key).
Please read the general Persistent documentation to learn how to create Unique keys.
Using this with an Entity without a Unique key leads to undefined behavior. A few of these functions require a single Unique, so using an Entity with multiple Uniques is also undefined. In these cases persistent's goal is to throw an exception as soon as possible, but persistent is still transitioning to that.
SQL backends automatically create uniqueness constraints, but for MongoDB you must manually place a unique index on a field to have a uniqueness constraint.
Methods
getBy :: (MonadIO m, PersistRecordBackend record backend) => Unique record -> ReaderT backend m (Maybe (Entity record))Get a record by unique key, if available. Returns also the identifier.
Example usage
getBySpjName :: MonadIO m => ReaderT SqlBackend m (Maybe (Entity User)) getBySpjName = getBy $ UniqueUserName "SPJ"mSpjEnt <- getBySpjNameThe above query when applied on dataset-1, will get this entity:
+----+------+-----+ | id | name | age | +----+------+-----+ | 1 | SPJ | 40 | +----+------+-----+existsBy :: (MonadIO m, PersistRecordBackend record backend) => Unique record -> ReaderT backend m BoolReturns True if a record with this unique key exists, otherwise False.
Example usage
existsBySpjName :: MonadIO m => ReaderT SqlBackend m Bool existsBySpjName = existsBy $ UniqueUserName "SPJ"spjEntExists <- existsBySpjNameThe above query when applied on dataset-1, will return the value True.
Instances4PersistUniqueRead
PersistUniqueRead SqlReadBackendDefined in persistent-2.14.6.3 · Database.Persist.Sql.Orphan.PersistUnique · orphanPersistUniqueRead SqlWriteBackendDefined in persistent-2.14.6.3 · Database.Persist.Sql.Orphan.PersistUnique · orphanPersistUniqueRead SqlBackendDefined in persistent-2.14.6.3 · Database.Persist.Sql.Orphan.PersistUnique · orphan(HasPersistBackend b, BackendCompatible b s, PersistUniqueRead b) => PersistUniqueRead (Compatible b s)Defined in persistent-2.14.6.3 · Database.Persist.Compatible.Types