Crecto
Search…
Crecto::Model
Application data models should inherit from Crecto::Model
1
class User < Crecto::Model
Copied!
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.

Schema

Inside the data model is where the database schema is defined.
1
class User < Crecto::Model
2
schema "users" do
3
field :first_name, String
4
field :last_name, String
5
end
6
end
Copied!
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

Default fields

By default Crecto assumes your table has the following column names defined id, created_at, and updated_at.
These can be easily overridden.
1
class User < Crecto::Model
2
set_created_at_field :other_field_name
3
set_updated_at_field nil # This table does not use an updated at field
4
5
# This table does not have a primary key field
6
schema "users", primary_key: false do
7
# fields...
8
end
9
end
Copied!

Fields

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 types

Int

1
field :first_field, Int32
2
field :second_field, Int64
3
field :third_field, Int16
Copied!

Float

1
field :first_field, Float32
2
field :second_field, Float64
Copied!

Bool

1
field :bool_field, Bool
Copied!

String

1
field :string_field, String
Copied!

Time

1
field :time_field, Time
Copied!

Array, postgres only

1
field :first_field, Array(Int32)
2
field :second_field, Array(Float64)
3
field :third_field, Array(Bool)
4
field :fourth_field, Array(String)
Copied!
Arrays can be queries using build-in postgres methods
1
Repo.all(User, Query.where("? = ANY(fourth_field)", "some_string"))
Copied!

Json, postgres only

1
field :json_field, Json
Copied!
Json fields can be both postgres json or jsonb types. If jsonb is used, those fields can be queried using build-in postgres methods.
1
Repo.all(User, Query.where("json_field @> '{\"test\": \"123\"}'")
Copied!

Field options

1
class User < Crecto::Model
2
# primary_key : Specify a new primary key field
3
field :uuid, String, primary_key: true
4
5
# virtual : This field does not exist in the database and wont be persisted.
6
# but can be set and accessed on the model instance
7
field :password_confirm, String, virtual: true
8
9
# default : Specify a default value for the attribute
10
field :is_admin, Bool, default: false
11
end
Copied!

Enum fields

1
class Vehicle < Crecto::Model
2
enum State
3
OFF
4
STARTING
5
RUNNING
6
end
7
8
enum Make
9
COUPE
10
SEDAN
11
HATCH
12
TRUCK
13
end
14
15
schema "vehicles" do
16
enum_field :state, State
17
enum_field :make, Make, column_name: "vehicle_type", column_type: Int32
18
end
19
end
Copied!

Non-nillable attributes

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
1
user.age!
2
user.name!
Copied!
Last modified 1yr ago