Trait DatabaseField

Source
pub trait DatabaseField: FromDbValue + ToDbFieldValue {
    const TYPE: ColumnType;
    const NULLABLE: bool = false;
}
Available on crate feature db only.
Expand description

A trait denoting that some type can be used as a field in a database.

Required Associated Constants§

Source

const TYPE: ColumnType

The type of the column in the database as one of the variants of the ColumnType enum.

§Changing the column type after initial implementation

Note that this should never be changed after the type is implemented. The migration generator is unable to detect a change in the column type and will not generate a migration for it. If the column type needs to be changed, a manual migration should be written, or a new type should be created.

This is especially important for types that are stored as fixed-length strings in the database, as the migration generator cannot detect a change in the string length. For this reason, it’s recommended to use the LimitedString type for fixed-length strings (which uses const generics, so each change in the length will be a new type) instead of a custom type with a fixed length.

Provided Associated Constants§

Source

const NULLABLE: bool = false

Whether the field can be NULL in the database.

This is false by default, meaning that the field is required in the database. This is set to true for Option types.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl DatabaseField for bool

Source§

const TYPE: ColumnType = ColumnType::Boolean

Source§

impl DatabaseField for f32

Source§

const TYPE: ColumnType = ColumnType::Float

Source§

impl DatabaseField for f64

Source§

const TYPE: ColumnType = ColumnType::Double

Source§

impl DatabaseField for i8

Source§

const TYPE: ColumnType = ColumnType::TinyInteger

Source§

impl DatabaseField for i16

Source§

const TYPE: ColumnType = ColumnType::SmallInteger

Source§

impl DatabaseField for i32

Source§

const TYPE: ColumnType = ColumnType::Integer

Source§

impl DatabaseField for i64

Source§

const TYPE: ColumnType = ColumnType::BigInteger

Source§

impl DatabaseField for u8

Source§

const TYPE: ColumnType = ColumnType::TinyUnsignedInteger

Source§

impl DatabaseField for u16

Source§

const TYPE: ColumnType = ColumnType::SmallUnsignedInteger

Source§

impl DatabaseField for u32

Source§

const TYPE: ColumnType = ColumnType::UnsignedInteger

Source§

impl DatabaseField for u64

Source§

const TYPE: ColumnType = ColumnType::BigUnsignedInteger

Source§

impl DatabaseField for String

Source§

const TYPE: ColumnType = ColumnType::Text

Source§

impl DatabaseField for Vec<u8>

Source§

const TYPE: ColumnType = ColumnType::Blob

Source§

impl DatabaseField for DateTime<FixedOffset>

Source§

const TYPE: ColumnType = ColumnType::DateTimeWithTimeZone

Source§

impl DatabaseField for NaiveDate

Source§

const TYPE: ColumnType = ColumnType::Date

Source§

impl DatabaseField for NaiveDateTime

Source§

const TYPE: ColumnType = ColumnType::DateTime

Source§

impl DatabaseField for NaiveTime

Source§

const TYPE: ColumnType = ColumnType::Time

Source§

impl<T: DatabaseField> DatabaseField for Option<T>

Source§

const NULLABLE: bool = true

Source§

const TYPE: ColumnType = T::TYPE

Implementors§

Source§

impl DatabaseField for PasswordHash

Source§

impl<T: DatabaseField> DatabaseField for Auto<T>

Source§

const NULLABLE: bool = T::NULLABLE

Source§

const TYPE: ColumnType = T::TYPE

Source§

impl<T: Model + Send + Sync> DatabaseField for ForeignKey<T>

Source§

const NULLABLE: bool = <T::PrimaryKey>::NULLABLE

Source§

const TYPE: ColumnType = <T::PrimaryKey>::TYPE

Source§

impl<const LIMIT: u32> DatabaseField for LimitedString<LIMIT>