class

Ralph::Migrations::Schema::TableDefinition

Inherits Reference / Object

Constructors

new(name : String, dialect : Dialect::Base = Dialect.current)
Source

Instance methods

array(name : String, element_type : Symbol = :text, null : Bool = true, default : String | Nil = nil)

Generic array column with custom element type

Options

  • element_type: Type of array elements (default: :text)
  • null: Allow NULL values (default: true)
  • default: Default value

Example

create_table :data do |t|
  t.array :values, element_type: :float, null: false
end
Source
belongs_to(name : String, polymorphic : Bool = false, null : Bool = true, foreign_key : String | Nil = nil, to_table : String | Nil = nil, on_delete : Symbol | Nil = nil, on_update : Symbol | Nil = nil, index : Bool = true)

Alias for reference (Rails compatibility)

Source
bigint(name : String, null : Bool = true, default : Int64 | Nil = nil)

Big integer column (64-bit)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
bigint_array(name : String, null : Bool = true, default : String | Nil = nil)

Bigint array column

  • PostgreSQL: BIGINT[]
  • SQLite: TEXT (JSON array)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
bigint_primary_key(name : String = "id", default : String | Nil = nil)

Bigint primary key (non-auto-incrementing)

Creates a BIGINT primary key without auto-increment. Useful when you need to control ID generation (e.g., distributed IDs, snowflake IDs).

For auto-incrementing integer primary keys, use primary_key() instead.

Options

  • name: Column name (default: "id")
  • default: SQL default expression

Example

create_table :events do |t|
  t.bigint_primary_key # Application generates IDs
  t.string :type
end
Source
binary(name : String, null : Bool = true, default : String | Nil = nil)

Binary/blob column

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
boolean(name : String, null : Bool = true, default : Bool | Nil = nil)

Boolean column

Options

  • null: Allow NULL values (default: true)
  • default: Default value (true/false)
Source
boolean_array(name : String, null : Bool = true, default : String | Nil = nil)

Boolean array column

  • PostgreSQL: BOOLEAN[]
  • SQLite: TEXT (JSON array)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
column(name : String, type : Symbol, **options)
Source
date(name : String, null : Bool = true, default : String | Int32 | Int64 | Float64 | Bool | Nil = nil)

Date column (date only, no time)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
decimal(name : String, precision : Int32 | Nil = nil, scale : Int32 | Nil = nil, null : Bool = true, default : String | Int32 | Int64 | Float64 | Bool | Nil = nil)

Decimal column (fixed precision)

Options

  • precision: Total number of digits
  • scale: Number of digits after decimal point
  • null: Allow NULL values (default: true)
  • default: Default value
Source
enum(name : String, values : Array(String), storage : Symbol = :string, null : Bool = true, default : String | Nil = nil)

Enum column - stores enum values in the database

Options

  • values: Array of allowed string values (required)
  • storage: Storage strategy - :string (default), :integer, or :native (PostgreSQL only)
  • null: Allow NULL values (default: true)
  • default: Default value

Example

create_table :users do |t|
  t.enum :status, values: ["active", "inactive", "suspended"], null: false
  t.enum :priority, values: ["low", "medium", "high"], storage: :integer
end
Source
expression_index(expression : String, name : String, unique : Bool = false, using : String | Nil = nil)

Add an expression index

Expression indexes index the result of an expression rather than a column.

Example

create_table :users do |t|
  t.string :email

  # Case-insensitive email lookup
  t.expression_index "lower(email)", name: "idx_users_email_lower", unique: true
end

Options

  • name: Index name (required)
  • unique: Create unique index (default: false)
  • using: Index method (e.g., "btree", "hash", "gin", "gist")
Source
expression_indexes
Source
float(name : String, null : Bool = true, default : Float64 | Nil = nil)

Float column (64-bit floating point)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
float_array(name : String, null : Bool = true, default : String | Nil = nil)

Float array column

  • PostgreSQL: DOUBLE PRECISION[]
  • SQLite: TEXT (JSON array)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
foreign_key(to_table : String, column : String | Nil = nil, primary_key : String = "id", on_delete : Symbol | Nil = nil, on_update : Symbol | Nil = nil, name : String | Nil = nil)

Add a foreign key constraint

Options

  • column: Source column (default: {to_table singularized}_id)
  • primary_key: Target column (default: "id")
  • on_delete: Action on delete - :cascade, :nullify, :restrict, :no_action
  • on_update: Action on update - :cascade, :nullify, :restrict, :no_action
  • name: Custom constraint name

Example

create_table :posts do |t|
  t.bigint :user_id, null: false
  t.foreign_key :users, on_delete: :cascade
end
Source
foreign_keys
Source
full_text_index(column : String, config : String = "english", name : String | Nil = nil, fastupdate : Bool = true)

Add a full-text search index (GIN on tsvector)

Creates a GIN index on a tsvector expression for efficient full-text search.

Example

create_table :articles do |t|
  t.string :title
  t.text :content

  # Single column
  t.full_text_index :title

  # Multiple columns
  t.full_text_index [:title, :content], config: "english"
end

Options

  • config: Text search configuration (default: "english")
  • name: Custom index name
  • fastupdate: Use fast update optimization (default: true)
Source
full_text_index(columns : Array(String), config : String = "english", name : String | Nil = nil, fastupdate : Bool = true)

Add a full-text search index on multiple columns

Source
full_text_indexes
Source
gin_index(column : String, name : String | Nil = nil, fastupdate : Bool = true)

Add a GIN index (Generalized Inverted Index)

GIN indexes are ideal for:

  • JSONB containment queries
  • Array overlap/containment
  • Full-text search

Example

create_table :posts do |t|
  t.jsonb :metadata
  t.string_array :tags
  t.gin_index :metadata
  t.gin_index :tags, name: "idx_posts_tags_gin"
end

Options

  • name: Custom index name (auto-generated if not provided)
  • fastupdate: Use fast update optimization (default: true). Set to false for better search performance at cost of slower writes.
Source
gin_indexes
Source
gist_index(column : String, name : String | Nil = nil)

Add a GiST index (Generalized Search Tree)

GiST indexes are ideal for:

  • Geometric data types
  • Range types
  • Nearest-neighbor searches

Example

create_table :locations do |t|
  t.float :latitude
  t.float :longitude
  t.gist_index "latitude", name: "idx_lat_gist"
end
Source
gist_index(columns : Array(String), name : String | Nil = nil)

Add a GiST index on multiple columns

Source
gist_indexes
Source
index(column : String, name : String | Nil = nil, unique : Bool = false)
Source
indexes
Source
integer(name : String, null : Bool = true, default : Int32 | Nil = nil)

Integer column (32-bit)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
integer_array(name : String, null : Bool = true, default : String | Nil = nil)

Integer array column

  • PostgreSQL: INTEGER[]
  • SQLite: TEXT (JSON array)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
json(name : String, null : Bool = true, default : String | Nil = nil)

JSON column (text-based JSON)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
jsonb(name : String, null : Bool = true, default : String | Nil = nil)

JSONB column (PostgreSQL binary JSON, TEXT on SQLite)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
partial_index(column : String, condition : String, name : String | Nil = nil, unique : Bool = false)

Add a partial index (index with WHERE condition)

Partial indexes only index rows matching the WHERE condition.

Example

create_table :users do |t|
  t.string :email
  t.boolean :active

  # Only index active users' emails
  t.partial_index :email, condition: "active = true", unique: true
end

Options

  • condition: SQL WHERE clause (required)
  • name: Custom index name
  • unique: Create unique index (default: false)
Source
partial_indexes
Source
postgres_indexes

Get all PostgreSQL-specific indexes

Source
primary_key(name : String, type : Symbol, default : String | Nil = nil)

Type-aware primary key

Creates a primary key column with the specified type. Useful for UUID, string, or other non-integer primary keys.

Options

  • type: Column type (:uuid, :string, :text, :integer, :bigint)
  • default: SQL default expression (e.g., "gen_random_uuid()" for PostgreSQL UUID)

Example

create_table :users do |t|
  t.primary_key "id", :uuid # UUID primary key
  t.string :name
end

create_table :settings do |t|
  t.primary_key "key", :string # String primary key
  t.text :value
end

Backend Behavior

| Type | SQLite | PostgreSQL | |------|--------|------------| | :uuid | CHAR(36) PRIMARY KEY NOT NULL | UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid() | | :string | TEXT PRIMARY KEY NOT NULL | TEXT PRIMARY KEY NOT NULL | | :integer | INTEGER PRIMARY KEY AUTOINCREMENT | SERIAL PRIMARY KEY | | :bigint | INTEGER PRIMARY KEY AUTOINCREMENT | BIGSERIAL PRIMARY KEY |

Source
primary_key(name = "id")

Auto-incrementing integer primary key (default)

Creates an auto-incrementing integer primary key column.

  • SQLite: INTEGER PRIMARY KEY AUTOINCREMENT
  • PostgreSQL: BIGSERIAL PRIMARY KEY

Example

create_table :users do |t|
  t.primary_key           # Creates "id" column
  t.primary_key "user_id" # Custom name
end
Source
reference(name : String, polymorphic : Bool = false, null : Bool = true, foreign_key : String | Nil = nil, to_table : String | Nil = nil, on_delete : Symbol | Nil = nil, on_update : Symbol | Nil = nil, index : Bool = true)

Reference column (foreign key column)

Creates a foreign key column pointing to another table.

Options

  • polymorphic: If true, creates both {name}_id and {name}_type columns
  • null: Allow NULL values (default: true)
  • foreign_key: Custom column name for the FK (default: {name}_id)
  • to_table: Target table name (default: pluralized name)
  • on_delete: Action on delete - :cascade, :nullify, :restrict, :no_action (default: nil)
  • on_update: Action on update - :cascade, :nullify, :restrict, :no_action (default: nil)
  • index: Add an index on the FK column (default: true)

Example

create_table :posts do |t|
  t.reference :user, null: false, on_delete: :cascade
  t.reference :author, to_table: :users, foreign_key: "author_id"
  t.reference :commentable, polymorphic: true
end
Source
references(name : String, polymorphic : Bool = false, null : Bool = true, foreign_key : String | Nil = nil, to_table : String | Nil = nil, on_delete : Symbol | Nil = nil, on_update : Symbol | Nil = nil, index : Bool = true)

Alias for reference (Rails compatibility)

Source
soft_deletes

Add deleted_at timestamp column for soft deletes

Use this in conjunction with the paranoid macro in your model to enable soft delete functionality.

Example

# Migration
create_table :users do |t|
  t.primary_key
  t.string :name
  t.timestamps
  t.soft_deletes # adds deleted_at column
end

# Model
class User < Ralph::Model
  table :users
  column id, Int64, primary: true
  column name, String
  timestamps
  paranoid # enables soft delete behavior
end
Source
string(name : String, size : Int32 = 255, null : Bool = true, default : String | Int32 | Int64 | Float64 | Bool | Nil = nil)

String column

Options

  • size: Maximum length (default: 255)
  • null: Allow NULL values (default: true)
  • default: Default value

Example

create_table :users do |t|
  t.string :name, size: 100, null: false
  t.string :email, null: false, default: ""
end
Source
string_array(name : String, null : Bool = true, default : String | Nil = nil)

String array column

  • PostgreSQL: TEXT[]
  • SQLite: TEXT (JSON array)

Options

  • null: Allow NULL values (default: true)
  • default: Default value

Example

create_table :posts do |t|
  t.string_array :tags, null: false, default: "[]"
end
Source
string_primary_key(name : String = "id", default : String | Nil = nil)

String primary key

Creates a TEXT primary key column. Useful for natural keys like slugs, codes, or external identifiers.

Options

  • name: Column name (default: "id")
  • default: SQL default expression

Example

create_table :settings do |t|
  t.string_primary_key "key"
  t.text :value
end

create_table :countries do |t|
  t.string_primary_key "code" # e.g., "US", "GB"
  t.string :name
end
Source
text(name : String, null : Bool = true, default : String | Int32 | Int64 | Float64 | Bool | Nil = nil)

Text column (unlimited length string)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
timestamp(name : String, null : Bool = true, default : String | Int32 | Int64 | Float64 | Bool | Nil = nil)

Timestamp column (date and time)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
timestamps

Add created_at and updated_at timestamp columns

Both columns allow NULL by default. Use timestamps_not_null for NOT NULL columns.

Source
timestamps_not_null

Add created_at and updated_at timestamp columns with NOT NULL constraint

Source
to_sql
Source
uuid(name : String, null : Bool = true, default : String | Nil = nil)

UUID column

Options

  • null: Allow NULL values (default: true)
  • default: Default value (use "gen_random_uuid()" for PostgreSQL auto-generation)
Source
uuid_array(name : String, null : Bool = true, default : String | Nil = nil)

UUID array column

  • PostgreSQL: UUID[]
  • SQLite: TEXT (JSON array)

Options

  • null: Allow NULL values (default: true)
  • default: Default value
Source
uuid_primary_key(name : String = "id", default : String | Nil = nil)

UUID primary key

Creates a UUID primary key column. This is a convenience method for primary_key("id", :uuid).

  • SQLite: Stored as CHAR(36), requires application-level UUID generation
  • PostgreSQL: Native UUID type with automatic generation via gen_random_uuid()

Options

  • name: Column name (default: "id")
  • default: SQL default expression. PostgreSQL defaults to "gen_random_uuid()". For SQLite, UUIDs must be generated in application code.

Example

create_table :users do |t|
  t.uuid_primary_key
  t.string :email
end

create_table :api_keys do |t|
  t.uuid_primary_key "key_id"
  t.string :name
end
Source