TypeMismatchError¶
class
Defined in src/ralph/errors.cr:413
Raised when a column type from the database doesn't match the model's expected type
This wraps DB::ColumnTypeMismatchError with additional context about which
model column was being read and helpful hints for fixing the type mismatch.
Example¶
Supply.find(id)
# => Ralph::TypeMismatchError: Type mismatch reading Supply#stock_quantity
#
# Column: stock_quantity (index 6)
# Expected type: Float64 | PG::Numeric | Nil
# Actual type: Float32
#
# Hint: PostgreSQL 'real' type maps to Float32 in Crystal.
# Change your model column from `Float64` to `Float32`:
#
# column stock_quantity : Float32
#
# Or change your database column to 'double precision' or 'numeric'.
Constants¶
TYPE_HINTS¶
TYPE_HINTS = {"Float32" => {"db_types" => ["real", "float4"], "crystal" => "Float32", "alternative" => "double precision or numeric"}, "Float64" => {"db_types" => ["double precision", "float8"], "crystal" => "Float64", "alternative" => "real (but loses precision)"}, "PG::Numeric" => {"db_types" => ["numeric", "decimal"], "crystal" => "Float64 (or PG::Numeric for exact precision)", "alternative" => "double precision (but loses exact precision)"}, "Int32" => {"db_types" => ["integer", "int4", "serial"], "crystal" => "Int32", "alternative" => "bigint for larger values"}, "Int64" => {"db_types" => ["bigint", "int8", "bigserial"], "crystal" => "Int64", "alternative" => "integer if values fit in 32 bits"}}
Common PostgreSQL type to Crystal type mappings for hints