Derive Macro diesel::sql_types::SqlType

source ·
#[derive(SqlType)]
{
    // Attributes available to this derive:
    #[diesel]
    #[postgres]
    #[sqlite_type]
    #[mysql_type]
}
Expand description

Implement necessary traits for adding a new sql type

This trait implements all necessary traits to define a new sql type. This is useful for adding support for unsupported or custom types on sql side. The sql type will be usable for all backends you specified via the attributes listed below.

This derive will implement NotNull, HasSqlType and SingleValue. When using this derive macro, you need to specify how the type is represented on various backends. You don’t need to specify every backend, only the ones supported by your type.

For PostgreSQL, add #[diesel(postgres_type(name = "pg_type_name", schema = "pg_schema_name"))] or #[diesel(postgres_type(oid = "some_oid", array_oid = "some_oid"))] for builtin types. For MySQL, specify which variant of MysqlType should be used by adding #[diesel(mysql_type(name = "Variant"))]. For SQLite, specify which variant of SqliteType should be used by adding #[diesel(sqlite_type(name = "Variant"))].

Attributes

Type attributes

  • #[diesel(postgres_type(name = "TypeName", schema = "public"))] specifies support for a postgresql type with the name TypeName in the schema public. Prefer this variant for types with no stable OID (== everything but the builtin types). It’s possible to leaf of the schema part. In that case diesel defaults to the default postgres search path.
  • #[diesel(postgres_type(oid = 42, array_oid = 142))], specifies support for a postgresql type with the given oid and array_oid. This variant should only be used with types that have a stable OID.
  • #[diesel(sqlite_type(name = "TypeName"))], specifies support for a sqlite type with the given name. TypeName needs to be one of the possible values in SqliteType
  • #[diesel(mysql_type(name = "TypeName"))], specifies support for a mysql type with the given name. TypeName needs to be one of the possible values in MysqlType