Application data models should inherit from Crecto::Model
class User < Crecto::Model
The Crecto::Model
class imposes the inherited
which imports the correct schema modules and sets up class wide variables and constants used for keeping track of associations and field names.
Inside the data model is where the database schema is defined.
class User < Crecto::Modelschema "users" dofield :first_name, Stringfield :last_name, Stringendend
The schema macro takes one argument and an options tuple. The argument is the database table name. If your database table doesn't have a primary key, you can pass an option to disable it: schema "users", primary_key: false do
By default Crecto assumes your table has the following column names defined id
, created_at
, and updated_at
.
These can be easily overridden.
class User < Crecto::Modelset_created_at_field :other_field_nameset_updated_at_field nil # This table does not use an updated at field# This table does not have a primary key fieldschema "users", primary_key: false do# fields...endend
Database table fields are defined using the field
macro. The field macro takes 2 arguments, the column name as a Symbol and the column type, and an optional options tuple.
field :first_field, Int32field :second_field, Int64field :third_field, Int16
field :first_field, Float32field :second_field, Float64
field :bool_field, Bool
field :string_field, String
field :time_field, Time
field :first_field, Array(Int32)field :second_field, Array(Float64)field :third_field, Array(Bool)field :fourth_field, Array(String)
Arrays can be queries using build-in postgres methods
Repo.all(User, Query.where("? = ANY(fourth_field)", "some_string"))
field :json_field, Json
Json
fields can be both postgres json
or jsonb
types. If jsonb
is used, those fields can be queried using build-in postgres methods.
Repo.all(User, Query.where("json_field @> '{\"test\": \"123\"}'")
class User < Crecto::Model# primary_key : Specify a new primary key fieldfield :uuid, String, primary_key: true# virtual : This field does not exist in the database and wont be persisted.# but can be set and accessed on the model instancefield :password_confirm, String, virtual: true# default : Specify a default value for the attributefield :is_admin, Bool, default: falseend
class Vehicle < Crecto::Modelenum StateOFFSTARTINGRUNNINGendenum MakeCOUPESEDANHATCHTRUCKendschema "vehicles" doenum_field :state, Stateenum_field :make, Make, column_name: "vehicle_type", column_type: Int32endend
If you with to access attributes of a model without having to check for nil
, in the case that you are using a NOT NULL
database constraint you can use the non-nillable attribute accessors.
CAUTION: Mis-use of this could lead to Nil reference runtime exceptions
user.age!user.name!