HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulepersistent-2.14.6.3Haskell2010

Database.Persist.Types

This module exports many types and functions for operating on persistent's database representation. It's a bit of a kitchen sink. In the future, this module will be reorganized, and many of the dependent modules will be viewable on their own for easier documentation and organization.

  • 30 types
  • 4 values

Various Types of Names

0 declarations

There are so many kinds of names. persistent defines newtype wrappers for Text so you don't confuse what a name is and what it is supposed to be used for

Database Definitions

0 declarations

Entity/Table Definitions

The EntityDef type is used by persistent to generate Haskell code, generate database migrations, and maintain metadata about entities. These are generated in the call to mkPersist.

Field definitions

The FieldDef type is used to describe how a field should be represented at the Haskell and database layers.

Intermediate Values

0 declarations

The PersistValue type is used as an intermediate layer between database and Haskell types.

Other Useful Stuff

9 declarations
datadata Filter record
#

Filters which are available for select, updateWhere and deleteWhere. Each filter constructor specifies the field being filtered on, the type of comparison applied (equals, not equals, etc) and the argument for the comparison.

Persistent users use combinators to create these.

Note that it's important to be careful about the PersistFilter that you are using, if you use this directly. For example, using the In PersistFilter requires that you have an array- or list-shaped EntityField. It is possible to construct values using this that will create malformed runtime values.

Constructors

familydata family Key record
#

By default, a backend will automatically generate the key Instead you can specify a Primary key made up of unique values.

Instances1RawSql
datadata Entity record
#

Datatype that represents an entity, with both its Key and its Haskell record representation.

When using a SQL-based backend (such as SQLite or PostgreSQL), an Entity may take any number of columns depending on how many fields it has. In order to reconstruct your entity on the Haskell side, persistent needs all of your entity columns and in the right order. Note that you don't need to worry about this when using persistent's API since everything is handled correctly behind the scenes.

However, if you want to issue a raw SQL command that returns an Entity, then you have to be careful with the column order. While you could use SELECT Entity.* WHERE ... and that would work most of the time, there are times when the order of the columns on your database is different from the order that persistent expects (for example, if you add a new field in the middle of you entity definition and then use the migration code -- persistent will expect the column to be in the middle, but your DBMS will put it as the last column). So, instead of using a query like the one above, you may use rawSql (from the Database.Persist.Sql module) with its /entity selection placeholder/ (a double question mark ??). Using rawSql the query above must be written as SELECT ?? WHERE ... Then rawSql will replace ?? with the list of all columns that we need from your entity in the right order. If your query returns two entities (i.e. (Entity backend a, Entity backend b)), then you must you use SELECT ??, ?? WHERE ..., and so on.

Constructors

Instances10Eq, Ord, Read, Show, Generic, SafeToInsert, …
newtypenewtype OverflowNatural
#

Prior to persistent-2.11.0, we provided an instance of PersistField for the Natural type. This was in error, because Natural represents an infinite value, and databases don't have reasonable types for this.

The instance for Natural used the Int64 underlying type, which will cause underflow and overflow errors. This type has the exact same code in the instances, and will work seamlessly.

A more appropriate type for this is the Word series of types from Data.Word. These have a bounded size, are guaranteed to be non-negative, and are quite efficient for the database to store.

Instances6Eq, Num, Ord, Show, PersistField, PersistFieldSql
  • Eq OverflowNaturalDefined in persistent-2.14.6.3 · Database.Persist.Class.PersistField
  • Num OverflowNaturalDefined in persistent-2.14.6.3 · Database.Persist.Class.PersistField
  • Ord OverflowNaturalDefined in persistent-2.14.6.3 · Database.Persist.Class.PersistField
  • Show OverflowNaturalDefined in persistent-2.14.6.3 · Database.Persist.Class.PersistField
  • PersistField OverflowNaturalDefined in persistent-2.14.6.3 · Database.Persist.Class.PersistField
  • PersistFieldSql OverflowNaturalDefined in persistent-2.14.6.3 · Database.Persist.Sql.Class

    This type uses the SqlInt64 version, which will exhibit overflow and underflow behavior. Additionally, it permits negative values in the database, which isn't ideal.

The rest of the types

31 declarations
datadata FieldDef
#

A FieldDef represents the inormation that persistent knows about a field of a datatype. This includes information used to parse the field out of the database and what the field corresponds to.

Constructors

Instances5Eq, Ord, Read, Show, Lift
  • Eq FieldDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Ord FieldDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Read FieldDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Show FieldDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Lift FieldDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
datadata PersistValue
#

A raw value which can be stored in any backend and can be marshalled to and from a PersistField.

Constructors

Instances12Eq, Ord, Read, Show, NFData, FromJSON, …
patternpattern PersistDbSpecific :: ByteString -> PersistValue
#

Deprecated. Deprecated since 2.11 because of inconsistent escaping behavior across backends. The Postgres backend escapes these values, while the MySQL backend does not. If you are using this, please switch to PersistLiteral_ and provide a relevant LiteralType for your conversion.

This pattern synonym used to be a data constructor for the PersistValue type. It was changed to be a pattern so that JSON-encoded database values could be parsed into their corresponding values. You should not use this, and instead prefer to pattern match on PersistLiteral_ directly.

If you use this, it will overlap a patern match on the 'PersistLiteral_, PersistLiteral, and PersistLiteralEscaped patterns. If you need to disambiguate between these constructors, pattern match on PersistLiteral_ directly.

datadata CascadeAction
#

An action that might happen on a deletion or update on a foreign key change.

Instances5Eq, Ord, Read, Show, Lift
datadata Checkmark
#

A Checkmark should be used as a field type whenever a uniqueness constraint should guarantee that a certain kind of record may appear at most once, but other kinds of records may appear any number of times.

NOTE: You need to mark any Checkmark fields as nullable (see the following example).

For example, suppose there's a Location entity that represents where a user has lived:

Location
    user    UserId
    name    Text
    current Checkmark nullable

    UniqueLocation user current

The UniqueLocation constraint allows any number of Inactive Locations to be current. However, there may be at most one current Location per user (i.e., either zero or one per user).

This data type works because of the way that SQL treats NULLable fields within uniqueness constraints. The SQL standard says that NULL values should be considered different, so we represent Inactive as SQL NULL, thus allowing any number of Inactive records. On the other hand, we represent Active as TRUE, so the uniqueness constraint will disallow more than one Active record.

Note: There may be DBMSs that do not respect the SQL standard's treatment of NULL values on uniqueness constraints, please check if this data type works before relying on it.

The SQL BOOLEAN type is used because it's the smallest data type available. Note that we never use FALSE, just TRUE and NULL. Provides the same behavior Maybe () would if () was a valid PersistField.

Constructors

  • Active

    When used on a uniqueness constraint, there may be at most one Active record.

  • Inactive

    When used on a uniqueness constraint, there may be any number of Inactive records.

Instances11Bounded, Enum, Eq, Ord, Read, Show, …
datadata CompositeDef
#
Instances5Eq, Ord, Read, Show, Lift
  • Eq CompositeDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Ord CompositeDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Read CompositeDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Show CompositeDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Lift CompositeDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
datadata EmbedEntityDef
#

An EmbedEntityDef is the same as an EntityDef But it is only used for fieldReference so it only has data needed for embedding

Instances5Eq, Ord, Read, Show, Lift
datadata EmbedFieldDef
#

An EmbedFieldDef is the same as a FieldDef But it is only used for embeddedFields so it only has data needed for embedding

Instances5Eq, Ord, Read, Show, Lift
datadata FieldAttr
#

Attributes that may be attached to fields that can affect migrations and serialization in backend-specific ways.

While we endeavor to, we can't forsee all use cases for all backends, and so FieldAttr is extensible through its constructor FieldAttrOther.

Constructors

  • FieldAttrMaybe

    The Maybe keyword goes after the type. This indicates that the column is nullable, and the generated Haskell code will have a Maybe type for it.

    Example:

    User
        name Text Maybe
    
  • FieldAttrNullable

    This indicates that the column is nullable, but should not have a Maybe type. For this to work out, you need to ensure that the PersistField instance for the type in question can support a PersistNull value.

    data What = NoWhat | Hello Text
    
    instance PersistField What where
        fromPersistValue PersistNull =
            pure NoWhat
        fromPersistValue pv =
            Hello $ fromPersistValue pv
    
    instance PersistFieldSql What where
        sqlType _ = SqlString
    
    User
        what What nullable
    
  • FieldAttrMigrationOnly

    This tag means that the column will not be present on the Haskell code, but will not be removed from the database. Useful to deprecate fields in phases.

    You should set the column to be nullable in the database. Otherwise, inserts won't have values.

    User
        oldName Text MigrationOnly
        newName Text
    
  • FieldAttrSafeToRemove

    A SafeToRemove attribute is not present on the Haskell datatype, and the backend migrations should attempt to drop the column without triggering any unsafe migration warnings.

    Useful after you've used MigrationOnly to remove a column from the database in phases.

    User
        oldName Text SafeToRemove
        newName Text
    
  • FieldAttrNoreference

    This attribute indicates that we should not create a foreign key reference from a column. By default, persistent will try and create a foreign key reference for a column if it can determine that the type of the column is a Key entity or an EntityId and the Entity's name was present in mkPersist.

    This is useful if you want to use the explicit foreign key syntax.

    Post
        title    Text
    
    Comment
        postId   PostId      noreference
        Foreign Post fk_comment_post postId
    
  • FieldAttrReference Text

    This is set to specify precisely the database table the column refers to.

    Post
        title    Text
    
    Comment
        postId   PostId references="post"
    

    You should not need this - persistent should be capable of correctly determining the target table's name. If you do need this, please file an issue describing why.

  • FieldAttrConstraint Text

    Specify a name for the constraint on the foreign key reference for this table.

    Post
        title    Text
    
    Comment
        postId   PostId constraint="my_cool_constraint_name"
    
  • FieldAttrDefault Text

    Specify the default value for a column.

    User
        createdAt    UTCTime     default="NOW()"
    

    Note that a default= attribute does not mean you can omit the value while inserting.

  • FieldAttrSqltype Text

    Specify a custom SQL type for the column. Generally, you should define a custom datatype with a custom PersistFieldSql instance instead of using this.

    User
        uuid     Text    sqltype=UUID
    
  • FieldAttrMaxlen Integer

    Set a maximum length for a column. Useful for VARCHAR and indexes.

    User
        name     Text    maxlen=200
    
        UniqueName name
    
  • FieldAttrSql Text

    Specify the database name of the column.

    User
        blarghle     Int     sql="b_l_a_r_g_h_l_e"
    

    Useful for performing phased migrations, where one column is renamed to another column over time.

  • FieldAttrOther Text

    A grab bag of random attributes that were unrecognized by the parser.

Instances5Eq, Ord, Read, Show, Lift
  • Eq FieldAttrDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Ord FieldAttrDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Read FieldAttrDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Show FieldAttrDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Lift FieldAttrDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
datadata FieldCascade
#

This datatype describes how a foreign reference field cascades deletes or updates.

This type is used in both parsing the model definitions and performing migrations. A Nothing in either of the field values means that the user has not specified a CascadeAction. An unspecified CascadeAction is defaulted to Restrict when doing migrations.

Instances5Eq, Ord, Read, Show, Lift
  • Eq FieldCascadeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Ord FieldCascadeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Read FieldCascadeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Show FieldCascadeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Lift FieldCascadeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
datadata FieldType
#

A FieldType describes a field parsed from the QuasiQuoter and is used to determine the Haskell type in the generated code.

name Text parses into FTTypeCon Nothing Text

name T.Text parses into FTTypeCon (Just T Text)

name (Jsonb User) parses into:

FTApp (FTTypeCon Nothing Jsonb) (FTTypeCon Nothing User)
Instances5Eq, Ord, Read, Show, Lift
  • Eq FieldTypeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Ord FieldTypeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Read FieldTypeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Show FieldTypeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Lift FieldTypeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
datadata ForeignDef
#
Instances5Eq, Ord, Read, Show, Lift
  • Eq ForeignDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Ord ForeignDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Read ForeignDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Show ForeignDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Lift ForeignDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
datadata LiteralType
#

A type that determines how a backend should handle the literal.

Constructors

  • Escaped

    The accompanying value will be escaped before inserting into the database. This is the correct default choice to use.

  • Unescaped

    The accompanying value will not be escaped when inserting into the database. This is potentially dangerous - use this with care.

  • DbSpecific

    The DbSpecific constructor corresponds to the legacy PersistDbSpecific constructor. We need to keep this around because old databases may have serialized JSON representations that reference this. We don't want to break the ability of a database to load rows.

Instances4Eq, Ord, Read, Show
  • Eq LiteralTypeDefined in persistent-2.14.6.3 · Database.Persist.PersistValue
  • Ord LiteralTypeDefined in persistent-2.14.6.3 · Database.Persist.PersistValue
  • Read LiteralTypeDefined in persistent-2.14.6.3 · Database.Persist.PersistValue
  • Show LiteralTypeDefined in persistent-2.14.6.3 · Database.Persist.PersistValue
datadata ReferenceDef
#

There are 3 kinds of references 1) composite (to fields that exist in the record) 2) single field 3) embedded

Constructors

Instances5Eq, Ord, Read, Show, Lift
  • Eq ReferenceDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Ord ReferenceDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Read ReferenceDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Show ReferenceDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Lift ReferenceDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
datadata SqlType
#

A SQL data type. Naming attempts to reflect the underlying Haskell datatypes, eg SqlString instead of SqlVarchar. Different SQL databases may have different translations for these types.

Instances5Eq, Ord, Read, Show, Lift
  • Eq SqlTypeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Ord SqlTypeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Read SqlTypeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Show SqlTypeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Lift SqlTypeDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
datadata UniqueDef
#

Type for storing the Uniqueness constraint in the Schema. Assume you have the following schema with a uniqueness constraint:

Person
  name String
  age Int
  UniqueAge age

This will be represented as:

UniqueDef
    { uniqueHaskell = ConstraintNameHS (packPTH UniqueAge)
    , uniqueDBName = ConstraintNameDB (packPTH "unique_age")
    , uniqueFields = [(FieldNameHS (packPTH "age"), FieldNameDB (packPTH "age"))]
    , uniqueAttrs = []
    }
Instances5Eq, Ord, Read, Show, Lift
  • Eq UniqueDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Ord UniqueDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Read UniqueDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Show UniqueDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Lift UniqueDefDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
datadata WhyNullable
#

The reason why a field is nullable is very important. A field that is nullable because of a Maybe tag will have its type changed from A to Maybe A. OTOH, a field that is nullable because of a nullable tag will remain with the same type.

Instances2Eq, Show
  • Eq WhyNullableDefined in persistent-2.14.6.3 · Database.Persist.Types.Base
  • Show WhyNullableDefined in persistent-2.14.6.3 · Database.Persist.Types.Base