macro primary_key(type_declaration)
#
class Avram::Migrator::CreateTableStatement
Included Modules
Defined in:
avram/migrator/create_table_statement.crConstructors
Macro Summary
- add(type_declaration, default = nil, index = false, unique = false, using = :btree, **type_options)
-
add_belongs_to(type_declaration, on_delete, references = nil, foreign_key_type = Int64, unique = false, index = true)
Adds a references column and index given a model class and references option.
- add_timestamps
- belongs_to(type_declaration, *args, **named_args)
- composite_primary_key(*columns)
- primary_key(type_declaration)
Instance Method Summary
-
#build(&) : CreateTableStatement
Accepts a block to build a table and indices using
add
andadd_index
methods. - #statements
Macros inherited from module Avram::Migrator::MissingOnDeleteWithBelongsToError
add_belongs_to(type_declaration, references = nil)
add_belongs_to
Instance methods inherited from module Avram::Migrator::IndexStatementHelpers
add_index(column : Symbol, unique = false, using : Symbol = :btree)
add_index,
index_added?(index : String, column : Symbol)
index_added?
Instance methods inherited from class Object
blank_for_validates_required? : Bool
blank_for_validates_required?
Constructor Detail
Macro Detail
macro add(type_declaration, default = nil, index = false, unique = false, using = :btree, **type_options)
#
macro add_belongs_to(type_declaration, on_delete, references = nil, foreign_key_type = Int64, unique = false, index = true)
#
Adds a references column and index given a model class and references option.
Instance Method Detail
def build(&) : CreateTableStatement
#
Accepts a block to build a table and indices using add
and add_index
methods.
The generated sql statements are aggregated in the #statements
method.
Usage
built = Avram::Migrator::CreateTableStatement.new(:users).build do
add_belongs_to Account, on_delete: :cascade
add :email : String, unique: true
end
built.statements
# => [
"CREATE TABLE users (
id serial PRIMARY KEY,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
account_id bigint NOT NULL REFERENCES accounts (id) ON DELETE CASCADE,
email text NOT NULL);",
"CREATE UNIQUE INDEX users_email_index ON users USING btree (email);"
]
An optional second argument can toggle between the usage of a numeric or uuid based id column.
built = Avram::Migrator::CreateTableStatement.new(:users, PrimaryKeyType::UUID).build do
add :email : String, unique: true
end