Safe Haskell | None |
---|---|
Language | Haskell2010 |
of this module will not have a corresponding major version bump.
Please depend on Database.Persist.ImplicitIdDef instead. If you can't use that module, please file an issue on GitHub with your desired use case.
Since: 2.13.0.0
Synopsis
- data ImplicitIdDef = ImplicitIdDef {
- iidFieldType :: EntityNameHS -> FieldType
- iidFieldSqlType :: SqlType
- iidType :: Bool -> Type -> Type
- iidDefault :: Maybe Text
- iidMaxLen :: Maybe Integer
- mkImplicitIdDef :: forall t. (Typeable t, PersistFieldSql t) => Text -> ImplicitIdDef
- setImplicitIdDefMaxLen :: Integer -> ImplicitIdDef -> ImplicitIdDef
- fieldTypeFromTypeable :: forall t. (PersistField t, Typeable t) => FieldType
- unsafeClearDefaultImplicitId :: ImplicitIdDef -> ImplicitIdDef
Documentation
data ImplicitIdDef Source #
A specification for how the implied ID columns are created.
By default, persistent
will give each table a default column named id
(customizable by PersistSettings
), and the column type will be whatever
you'd expect from
. For The BackendKey
yourBackendTypeSqlBackend
type,
this is an auto incrementing integer primary key.
You might want to give a different example. A common use case in postgresql is to use the UUID type, and automatically generate them using a SQL function.
Previously, you'd need to add a custom Id
annotation for each model.
User Id UUID default="uuid_generate_v1mc()" name Text Dog Id UUID default="uuid_generate_v1mc()" name Text user UserId
Now, you can simply create an ImplicitIdDef
that corresponds to this
declaration.
newtype UUID = UUIDByteString
instancePersistField
UUID wheretoPersistValue
(UUID bs) =PersistLiteral_
Escaped
bsfromPersistValue
pv = case pv of PersistLiteral_ Escaped bs -> Right (UUID bs) _ -> Left "nope" instancePersistFieldSql
UUID wheresqlType
_ =SqlOther
UUID
With this instance at the ready, we can now create our implicit definition:
uuidDef :: ImplicitIdDef uuidDef = mkImplicitIdDef @UUID "uuid_generate_v1mc()"
And we can use setImplicitIdDef
to use this with the MkPersistSettings
for our block.
mkPersist (setImplicitIdDef uuidDef sqlSettings) [persistLowerCase| ... |]
TODO: either explain interaction with mkMigrate or fix it. see issue #1249 for more details.
Since: 2.13.0.0
ImplicitIdDef | |
|
:: forall t. (Typeable t, PersistFieldSql t) | |
=> Text | The default expression to use for columns. Should be valid SQL in the language you're using. |
-> ImplicitIdDef |
Create an ImplicitIdDef
based on the Typeable
and PersistFieldSql
constraints in scope.
This function uses the TypeApplications
syntax. Let's look at an example
that works with Postgres UUIDs.
newtype UUID = UUID Text deriving newtype PersistField instance PersistFieldSql UUID where sqlType _ = SqlOther "UUID" idDef :: ImplicitIdDef idDef = mkImplicitIdDefTypeable @UUID "uuid_generate_v1mc()"
This ImplicitIdDef
will generate default UUID columns, and the database
will call the uuid_generate_v1mc()
function to generate the value for new
rows being inserted.
If the type t
is Text
or String
then a max_len
attribute of 200 is
set. To customize this, use setImplicitIdDefMaxLen
.
Since: 2.13.0.0
setImplicitIdDefMaxLen :: Integer -> ImplicitIdDef -> ImplicitIdDef Source #
Set the maximum length of the implied ID column. This is required for
any type where the associated SqlType
is a TEXT
or VARCHAR
sort of
thing.
Since: 2.13.0.0
fieldTypeFromTypeable :: forall t. (PersistField t, Typeable t) => FieldType Source #
This function converts a Typeable
type into a persistent
representation of the type of a field - FieldTyp
.
Since: 2.13.0.0
unsafeClearDefaultImplicitId :: ImplicitIdDef -> ImplicitIdDef Source #
Remove the default attribute of the ImplicitIdDef
column. This will
require you to provide an ID for the model with every insert, using
insertKey
instead of insert
, unless the type has some means of getting
around that in the migrations.
As an example, the Postgresql SERIAL
type expands to an autoincrementing
integer. Postgres will implicitly create the relevant series and set the
default to be NEXTVAL(
. A default is therefore unnecessary to
use for this type.series_name
)
However, for a UUID
, postgres *does not* have an implicit default. You must
either specify a default UUID generation function, or insert them yourself
(again, using insertKey
).
This function will be deprecated in the future when omiting the default implicit ID column is more fully supported.
Since: 2.13.0.0