module Clear::Model::HasSerialPkey

Direct including types

Defined in:

clear/model/modules/has_serial_pkey.cr

Constant Summary

PKEY_TYPE = {"bigserial" => "column(__name__ : Int64, primary: true, presence: false)", "serial" => "column(__name__ : Int32, primary: true, presence: false)", "text" => "column(__name__ : String, primary: true, presence: true)", "int" => "column(__name__ : Int32, primary: true, presence: true)", "bigint" => "column(__name__ : Int64, primary: true, presence: true)", "uuid" => "column(__name__ : UUID, primary: true, presence: true)\nbefore(:validate) do |m|\n if (!m.persisted?) && (m.as(self)).__name___column.value(nil).nil?\n (m.as(self)).__name__ = UUID.random\n end\nend\n"} of Nil => Nil

Macro Summary

Macro Detail

macro add_pkey_type(type, &block) #

Add a hook for the primary_key In the hook, name will be replaced by the column name required by calling primary_key

Example

Clear::Model::HasSerialPkey.add_pkey_type("awesomepkey") do
  column __name__ : AwesomePkey, primary: true, presence: false

  before_validate do
    # ...
  end
end

Your new primary key system can then be called using primary_key method:

primary_key :id, :awesomepkey

[View source]
macro primary_key(name = "id", type = :bigserial) #

Macro used to define serializable primary keys. Currently support bigserial, serial and uuid.

For bigserial and serial, let to PostgreSQL the handling of sequence numbers. For uuid, will generate a new UUID number on creation.


[View source]