primary_key(type_declaration)
SourceAvram::Migrator::CreateTableStatement
Inherits Avram::Migrator::MissingOnDeleteWithBelongsToError < Avram::Migrator::IndexStatementHelpers < Reference < Object
Constructors
Instance methods
build
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
Partitioning can be specified by passing a partition_by option to the block.
built = Avram::Migrator::CreateTableStatement.new(:events).build do
add id : Int64
add type : Int32
add :message : String
add_timestamps
composite_primary_key :id, :created_at
partition_by :created_at, type: :range
end
partition
Sourcestatements
SourceMacros
add(type_declaration, default = nil, index = false, unique = false, using = :btree, **type_options)
Sourceadd_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
Sourcebelongs_to(type_declaration, *args, **named_args)
Sourcecomposite_primary_key(*columns)
Sourcepartition_by(column, type)
Source