module Crecto::Schema

Overview

Schema is used to define the table name, and inside the schema block define the database field (column) names.

example:

class User < Crecto::Model
  schema "users" do
    field :name, String
  end
end

In the above example, the User class will map to the "users" database table. The :name field will map to a column in the "users" table named "name", which is a String (varchar)

Schema assumes the data store table also has id, created_at and updated_at at fields

field accepts some optional parameters

By default, Schema assumes id, created_at and updated_at fields exist in the database.

The primary key field can be changed from id as an option to field as stated above.

The timestamp fields can be changed or removed by changing:

Defined in:

crecto/schema/belongs_to.cr
crecto/schema/has_many.cr
crecto/schema/has_one.cr
crecto/schema.cr

Macro Summary

Macro Detail

macro created_at_field(val) #

Macro to change created_at field name


[View source]
macro enum_field(field_name, field_type, **opts) #

Enum field definition macro. opts can include column_name to set the name of the backing column in the database and column_type to set the type of that column (String and all Int types are currently supported)


[View source]
macro field(field_name, field_type, **opts) #

Field definitions macro


[View source]
macro schema(table_name) #

schema block macro


[View source]
macro updated_at_field(val) #

Macro to chnage updated_at field name


[View source]